title: Jest - Testing for AWS Lambda
body: <p>I'm trying to use Jest with my NodeJS based AWS Lambda function to test that the Lambda handler function is returning specific status codes. It seems like using <a href="https://www.npmjs.com/package/lambda-tester" rel="nofollow noreferrer">lambda-tester</a> is the easiest way to achieve this.</p>
  
  <p><strong>handler.test.js</strong></p>
  
  <pre><code>const handler = require('../src/handler.js');
  const LambdaTester = require('lambda-tester');
  
  test('should return a StatusCode of 400 due to requirement not being met', function (done) {
    return LambdaTester(handler.run)
      .event({})
      .expectResult(result =&gt; {
        expect(result.statusCode).toBe(400);
      });
  });
  </code></pre>
  
  <p>I thought that would be pretty straightforward however I get this as a result:</p>
  
  <pre><code>expect(received).toBe(expected) // Object.is equality
  
  Expected: 400
  Received: 500
  
     7 |     .event({})
     8 |     .expectResult(result =&gt; {
  &gt;  9 |       expect(result.statusCode).toBe(400);
       |                                 ^
    10 |     });
    11 | });
    12 | 
  
    at tests/handler.test.js:9:33
    at runVerifier (node_modules/lambda-tester/lib/runner.js:121:16)
    at LambdaRunner.run (node_modules/lambda-tester/lib/runner.js:277:23)
  </code></pre>
  
  <p>If I change the expect line to check for a statuscode of 500 I get an Async timeout:</p>
  
  <blockquote>
    <p>Timeout - Async callback was not invoked within the 5000 ms timeout
    specified by jest.setTimeout.Timeout</p>
  </blockquote>
  
  <p>Any ideas as to why this isn't working?</p>
  
  <p>The lambda should fail instantly because the event being passed in doesnt have a required attribute and it indeed does when tested via Postman.</p>
  
  <p><strong>Edit:</strong>
  This lambda is part of a serverless application and could be tested locally, how could I use Jest to do that?</p>