I'm looking to execute my end-to-end test post-deployment for the ability to revert in case of any issues. I've followed the guidelines outlined in this particular blog post.
Below is my lambda function:
export async function testLambda(event: APIGatewayEvent, context, callback) {
console.log('Initial version!')
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Initial version!'
})
}
return callback(null, response)
}
This is my post-hook logic:
export async function postHook(event, context, callback) {
const deploymentId = event.DeploymentId;
const lifecycleEventHookExecutionId = event.LifecycleEventHookExecutionId;
console.log(`deploymentId: ${deploymentId} - lifecycleEventHookExecutionId: ${lifecycleEventHookExecutionId}`)
try {
const jest = require('jest');
require('ts-jest');
const options = {
projects: [__dirname],
silent: true,
};
await jest.runCLI(options, options.projects);
// update AWS deployment for success
} catch (err) {
// update AWS deployment for failure
console.log(err);
return { statusCode: 500 }
}
}
My tests are written in TypeScript and I have included my jest.config.js and tsconfig.json in the serverless application zip file. However, I am facing an issue where global symbols like describe and expect from jest are not being recognized:
Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try
npm i @types/jest
ornpm i @types/mocha
and then addjest
ormocha
to the types field in your tsconfig.
What would be the best approach to run my tests using AWS Lambda? Currently, my only options seem to be triggering another task like CodeBuild to run the tests or switching to a different testing framework like Mocha. Is there a more optimal solution available?