CDK NodejsFunction Mock
The CDK NodejsFunction construct uses
Parcel and Docker to conveniently bundle up your lambda modules. However this is a slow and processor-intensive
operation, which results in very slow testing with e.g. Jest.This module mocks out NodeJsFunctions by replacing them with a standard CDK Lambda Function for the purpose of testing only.
Use with Jest
Whole Project
We recommend using this module across your entire CDK project, by editing your Jest config (e.g. in package.json) to add the following:{
"moduleNameMapper": {
"@aws-cdk/aws-lambda-nodejs": "cdk-lambda-nodejs-mock"
}
}
A Specific Test File Only
You can use in a specific test file only, rather than globally, as follows:jest.mock('@aws-cdk/aws-lambda-nodejs', () =>
require('cdk-lambda-nodejs-mock')
);
Note we use require rather than import, as
jest.mock
is hoisted above import.