Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

609
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda