Reputation: 3702
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?
Upvotes: 1
Views: 3578
Reputation: 4639
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.
Upvotes: 1