简体   繁体   中英

How to Mock AWS Lambda invoke

I have the following common.js code.

const AWS = require('aws-sdk');

exports.callNextLambda = function callNextLambda(lambdaName, payload) {
  const lambda = new AWS.Lambda();
  lambda.invoke({
    FunctionName: lambdaName,
    InvocationType: 'Event',
    Payload: payload,
  }, (err, data) => {
    if (err) console.log(err, err.stack);
    else console.log(data);
  });
}

I tried to mock above as,

it('MockAWS.mock should mock Lambda invoke', () => {
  const lambda = new AWS.Lambda();
  AWSMock.mock(lambda, 'invoke', (params, callback) => {
    const awsResponse = {
      LogResult:'some-data',
      Payload:'{\'contentType\':\'text/plain\',\'message\':\'some-other-data\',\'status\':200}',
      StatusCode:200
    };
    callback(null, awsResponse);
  })

});

The above code is throwing some weird error. And not covering the lines in code coverage. I am very much new to NodeJS. Can somebody please help?

I haven't used mock-aws before, but it looks like you're calling it wrong.

Change this:

AWSMock.mock(lambda, 'invoke', (params, callback) => {
  // logic here
})

to this:

AWSMock.mock('lambda', 'invoke', (params, callback) => {
  // logic here as normal
})

The first parameter must be a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM