aws-sdk-mock
aws-sdk-mock copied to clipboard
Mocking Lambda.invoke fails
The following code does not mock out the Lambda service. I'm using version 5.1.0.
const AWS = require('aws-sdk-mock');
AWS.mock('Lambda', 'invoke', { msg : 'success' });
In addition, the Readme should be updated to show a Lambda invoke example.
@jcald1 Pull Request to update the README.md
very much welcome. 👍
@nelsonic , sure, but I can do that after the underlying issue is identified/fixed. I see someone else is not able to mock Lambda invokes.
This seems to work for me on 5.1.0 so I don't think there's an issue. I could add an example to the readme if you want to but it's practically the same example as the DynamoDB one that's already there.
import AWS from 'aws-sdk';
import AWSMock from 'aws-sdk-mock';
describe('lambda mock', () => {
it('should be able to mock lambda invoke', async () => {
AWSMock.setSDKInstance(AWS);
AWSMock.mock('Lambda', 'invoke', (params: any, callback: any) => {
callback(null, { pk: 'foo', sk: 'bar' });
});
const input = { FunctionName: 'myFunction' };
const lambda = new AWS.Lambda();
expect(await lambda.invoke(input).promise()).toStrictEqual({ pk: 'foo', sk: 'bar' });
AWSMock.restore('Lambda');
});
});
@TastefulElk , so it seems that the mock function when mocking Dynamo and Lambda takes a callback, but when mocking S3 and SNS, it takes a value. I would list out in the Readme which mock services use values and which ones use callbacks.
@jcald1 No, either should be fine. This works as well for example:
AWSMock.mock('Lambda', 'invoke', { pk: 'foo', sk: 'bar' });
const input = { FunctionName: 'myFunction' };
const lambda = new AWS.Lambda();
expect(await lambda.invoke(input).promise()).toStrictEqual({ pk: 'foo', sk: 'bar' });
@TastefulElk, I'm wondering if just doesn't work when you test a separate imported module that is the one importing AWS/invoking a Lambda. I don't have access to the original code I had used, but https://github.com/dwyl/aws-sdk-mock/issues/215 also had the same issue.