Tuesday, 25 April 2017

Node unit test AWS Lambda function using context.succeed()

I am trying to test my lambda function locally, using mocha, chai and chai-as-promised where async is concerned. My lambda uses the AWS context.succeed format to return the result, not the callback, and I need to keep it that way. I have one test below, which returns a status code of 200, but when I expect it to be 400, it still passes the test (false positive). Any logs placed in the custom context succeed function, is not shown, indicating it is not getting hit, or I my understanding of how context.succeed works is a little off. Any pointers would be much appreciated.

Note - The lambda works just fine, the test is all I have an issue with.

Here is the simple code I have so far. Basically, I call my lambda function, pass in an event and context as expected. When context.succeed is called, it should test the result passed into context.

'use strict';
const myHandler = require('../lambdaHandler');
const chai = require('chai');
const expect = chai.expect;
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);

describe('myHandler.handler should return with expected statusCodes', () => {

it('should return a 200 statusCode', () => {
  myHandler.handler(event, {
    succeed : (data) => {
      expect(data).to.eventually.have.property('statusCode');
      expect(data.statusCode).to.eventually.equal(400);
    },
    fail : (data) => {
      expect(data).to.have.property('statusCode');
    },
  });
});


});



via jmcgui05

No comments:

Post a Comment