Stub DynamoDB in NodeJS for API

Question:

I have a problem to stub a call to DynamoDB for API testing. I’m using serverless mocha and want to test my API from an end-to-end perspective. The project structure is not straightforward, but inside the API file itself, I have a separate call to different class file to actually work with DynamoDB functions. Let’s assume it as a library.
So, I cannot use aws-sdk-mock since requirements “The AWS Service needs to be initialised inside the function being tested in order for the SDK method to be mocked” is not my case.
How I can test my API and to stub calls to DynamoDB?
Now test is actually working, but it makes real call to database which I want to avoid:

Answer:

I had the same issue, I resolved it by using sinon, please see how to stub you Dynamo function and then test whatever you want:

in this example, I stub the promise implementation I believe you can stub the put with a callback like this:

Leave a Reply