I use NodeJs to implement AWS Lambda function and want to clarify what is the right way to test integration with DynamoDB:
Lambda code:
const AWS = require('aws-sdk');
AWS.config.update({region: region});
const dynamoDb = new AWS.DynamoDB();
exports.handler = async function(event, context) {
...
await dynamoDb.deleteItem(item).promise();
}
I was going to use mocha, sinon, chai and aws-sdk-mock for tests:
const expect = require('chai').expect;
const AWS = require('aws-sdk-mock');
const lambda = require('../index.js');
const sinon = require('sinon');
describe('Test', function () {
let deleteItemSpy;
beforeEach(function () {
deleteItemSpy = sinon.spy();
AWS.mock('DynamoDB', 'deleteItem', deleteItemSpy);
}
it('valid call', async function() {
await lambda.handler({"id":1}, null);
expect(deleteItemSpy.calledOnce).to.be.true;
})
});
But there are two main problems:
Mock doesn't work if dynamoDb is created outside of the handler. What other options do I have? Can I use
sinon.stubsomehow?It throws timeout because
awaitnever receives the result from lambda. Problem is related tospyitself. I probably can replace it with:AWS.mock('DynamoDB', 'deleteItem', function (params, callback) { });