Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

606
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda