How to mock and unit test serverless lambda invoking AWS and Dynamodb

Suranjana Basu
Jun 13 · 2 min read
var AWS;
var getSomeItemStub
beforeEach
(() =>{
getSomeItemStub = sinon.stub();
AWS = {

DynamoDB: sinon.stub().returns({
getItem: getSomeItemStub
})

};
testLambda = proxyquire( '../index', {
'aws-sdk': AWS
});
getSomeItemStub.withArgs(sinon.match.any).yields(null, someObj);
it( 'test invocation', async() => {
.....
getSomeItemStub.withArgs(sinon.match.any).yields(null, someObj);
const result = await testLambda.handler({"eventKey": "eventValue"}, context, (err, result) => {

expect(result.test).to.exist;
.......
});
}
getSomeItemStub.resetHistory();
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade