Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

605
Views
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');
    },
  });
});


});
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I would suggest you read this article:

https://blog.atomdata.io/serverless-applications-continuous-delivery-with-aws-lambda-and-api-gateway-part-1-unit-tests-e517aa1cd09e

Which explains how these guys are unit testing their lambda's.

over 4 years ago · Santiago Trujillo Report

0

Here is what I would do:

'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', (done) => {
  try{ 
  myHandler.handler(event, {
    succeed : (data) => {
      expect(data).to.eventually.have.property('statusCode');
      expect(data.statusCode).to.eventually.equal(400);
      done();
    },
    fail : (data) => {
      expect(data).to.have.property('statusCode');
      done();
    },
  });
  } catch(err){
    done(err.message);
  }
});


});
over 4 years ago · Santiago Trujillo Report

0

I ended up going with lambda-tester: https://github.com/vandium-io/lambda-tester. The only downside is that it does not support Node version > 4.3.2

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!