Mock the aws-sdk using jest
Hi ✋,
The hardest part of writing the unit test is creating mocks for external libraries, the purpose of this article is to help someone who has problems mocking a library like this.
Today I'm going to code how to mock your lambda invoke function in 'aws-sdk' using a jest 💪.
I'm going to write test cases for the below code. This is a simple cache service provider. it contains a getter and a setter.
CacheServiceProvider.ts
In here, I want to mock lambda.invoke function and want to set different responses to the promise resolve/reject.
This is my CacheServiceProvider.test.ts
Here I have fakePromise function and I can use jest mockImplementation method when
I want to set response to the lambda.invoke().promise() 😁.
If you need to change promise response , then you have to mock invoke function like invoke: () => fakePromise in the jest.mock("aws-sdk").
If this article helped you, please leave a comment. and also please leave your suggestions.