Node.js Lambda function unit test using mocha/chai

93 views Asked by At

I have a Lambda function in a Node.js environment which returns a success response as follows:

{
  "moduleName": "getVVTags",
  "message": "[\"6.9.0\",\"6.8.0\",\"6.7.0\",\"6.6.0\",\"6.5.0\",\"6.4.5\",\"6.4.0\",\"6.3.0\",\"6.2.0\",\"6.0.1\",\"6.1.0\",\"6.0.0\",\"5.9.0\",\"4.3.0\",\"5.7.0\",\"5.8.0\",\"5.4.0\",\"5.5.0\",\"5.3.1\",\"5.6.0\",\"5.3.0\",\"5.1.5\",\"5.2.1\",\"5.2.0\",\"5.1.0\",\"5.0.0\",\"4.8.0\",\"4.9.0\",\"4.7.0\",\"4.6.0\",\"4.5.0\",\"4.3.1\",\"4.4.0\",\"4.1.1\",\"4.2.0\",\"4.1.0\",\"4.0.0\",\"3.0.0\",\"3.1.0\",\"2.1.0\",\"2.0.0\",\"1.0.0\",\"4.9.3\"]",
  "moduleType": "Lambda",
  "moduleVersion": "1.0",
  "language": "en",
  "memberId": "NA",
  "invokedFunctionArn": "arn:aws:lambda:us-east-1:280362372798:function:handler:1.0",
  "clientIP": "NA",
  "awsRequestId": "8d9db1be-04e1-26e5-01b0-2cff55576062",
  "errorDescription": "",
  "logEvent": "Response",
  "httpStatusCode": 200,
}

Now, I'm using mocha/chai for writing unit test for this Lambda:

const { describe } = require("mocha");
const chai = require("chai");
const { expect } = chai;
const getVVTagsLambdaHandler = require("../src/api/getVVTags/handler/GetVVTags");
const getVVTagsMockEvent = require("../mock/GetVVTags.json");

describe('getVVTags', () => {
    it('shoud return', () => {
        try {
            getVVTagsLambdaHandler(getVVTagsMockEvent, {
                succeed: result => {
                    console.log({result});
                    expect(result).to.be.a('object');
                    expect(result).to.not.be.empty;
                    expect(result.statusCode).to.be.equal(200);
                    expect(result.message).to.be.a("array");
                },
                fail: result => {
                    expect(result).to.have.property('httpStatusCode');
                    expect(result.httpStatusCode).to.not.be.equal(200);
                }
            });
        } catch (error) {
            // done(error.message);
        }
    });
});

When I run this test, following are my observations:

  • Test passes no matter what.
  • I do not see the console log print in the console.

I have tried packages like lambda-tester (which does not support my Node version) and lambda-local.

I'm unable to understand how this functions, and why am I getting unexpected results. Am I mistaken in the way unit tests are written for Lambda functions?

0

There are 0 answers