title: Unit Testing Using Jest and AWS-SDK-Mock, Mock DynamoDB Scan
body: <p>Jumping into Jest and unit testing lambda functions.  I'm trying to mock the aws dynamodb-document client scan in my unit test.  I'm getting back the actual scan from the real db though so i know something with my mock isn't working.  This is my .test.js:</p>
  <pre><code>var AWS = require('aws-sdk-mock');
  const sinon = require('sinon');
  let index = require('./index.js');
  
  
  beforeEach(()=&gt; {
      AWS.mock('DynamoDB.DocumentClient', 'scan', function(params, callback) {
          callback(null, {test: &quot;value&quot;});
      })
    });
  
    afterEach(()=&gt; {
        AWS.restore();
    });
  
      it( 'test invocation', async() =&gt; {
          console.log(&quot;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!&quot;)
        const result = await index.handler({}, {}, (err, result) =&gt; {
            expect(result).toEqual({test: &quot;value&quot;});
    });
    });
  </code></pre>
  <p>My actual lambda function code looks like this:</p>
  <pre><code>'use strict';
  
  var AWS = require(&quot;aws-sdk&quot;);
  var DOC = require(&quot;dynamodb-doc&quot;);
  AWS.config.update({region: &quot;us-east-1&quot;//,
      });
  var docClient = new AWS.DynamoDB.DocumentClient();
  var params = {
      TableName: &quot;table-name&quot;,
      FilterExpression: &quot;#PK = :cat_val&quot;,
      ExpressionAttributeNames: {&quot;#PK&quot;: &quot;PK&quot;,},
      ExpressionAttributeValues: { &quot;:cat_val&quot;: &quot;Value&quot;}
  };
  
  exports.handler = (event, context, callback) =&gt; {
      var response; 
      
      docClient.scan(params, onScan);
      function onScan(err, data) {
          if (err) {
              response = {
                  statusCode: 500,
                  body: &quot;Unable to complete scan.  Error JSON: &quot; + JSON.stringify(err,null,2)
              }
          } else {
              data.Items.sort(compare);
              response = {
                  statusCode: 200,
                  body: data.Items
              }
          }
  
          callback(null, response);
          console.log(response);
      }
  }
  </code></pre>
  <p>Any direction you guys can point me in.  The test fails on the equal compare because it has the actual response from the Dynamo scan.</p>
  <p>Thanks,
  Tim</p>