I am trying to mock an Lambda.invoke() call in my Jest. However, the mock call didn't work and instead executed the real invoke() method which leads to not authorized to perform: lambda:InvokeFunction
. I couldn't find out what's wrong as I did the same for mocking DynamoDB.DocumentClient and it works without any issue.
Jest File:
describe('Mock Lambda', () => {
beforeAll(async () => {
AWSMock.mock('Lambda', 'invoke', (params, callback) => {
if (params.FunctionName === 'MyLambaInvocation') {
callback(null, { status: 200, data: { code: '0', message: 'Successful' }
}
});
const result = await (myTest.handler(event, context(), null) as Promise<APIGatewayProxyResult>);
});
Typescript File:
import { Lambda } from 'aws-sdk';
export function invokeLambda(eventObject) {
return new Promise<any>((resolve, reject) => {
const lambdaConfig = {
region: 'ap-southeast-1',
endpoint: process.env.IS_OFFLINE ? 'http://localhost:8080' : undefined
};
const lambda = new Lambda(lambdaConfig);
const params = {
FunctionName: 'MyLambaInvocation',
Payload: JSON.stringify(eventObject)
};
lambda.invoke(params, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}