title: How to test await/async with DynamoDb on nodejs?
body: <p>I use NodeJs to implement AWS Lambda function and want to clarify what is the right way to test integration with DynamoDB:</p>
  
  <p>Lambda code: </p>
  
  <pre><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();
  }
  </code></pre>
  
  <p>I was going to use <code>mocha, sinon, chai and aws-sdk-mock</code> for tests:</p>
  
  <pre><code>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;
    })
  });
  </code></pre>
  
  <p>But there are two main problems:</p>
  
  <ol>
  <li><p>Mock doesn't work if dynamoDb is created outside of the handler. What other options do I have? Can I use <code>sinon.stub</code> somehow?</p></li>
  <li><p>It throws timeout because <code>await</code> never receives the result from lambda. Problem is related to <code>spy</code> itself. I probably can replace it with: <code>AWS.mock('DynamoDB', 'deleteItem', function (params, callback) { });</code></p></li>
  </ol>