Add CDK snapshot tests to compare the entire synthesized CloudFormation template.
When creating snapshot tests, follow these principles:
- use snapshot tests to catch unexpected changes in the overall infrastructure.
- consider snapshot tests as a change detection mechanism rather than a regression testing tool.
- ensure that the snapshot test covers CDK stacks, not individual L3 constructs.
- if applicable, mock CDK assets like docker images or lambda function to prevent generating zip files during test execution. This speeds up test execution and ensures static keys for assets so that snapshots do not change on every execution.
The snapshot test should conform to the following guidelines:
- use jest as the test framework
- add comments to explain what every test is covering
- use a meaningful name of the test
- do not add extra
describeblocks. - use the
test()method to run an individual test - do not import
describe,test,jestorexpect. - use the
aws-cdk-lib.assertions libraryfrom CDK v2 for assertions - use single named imports
- use arrow functions for all
testfunctions. - the test file must be named like the source ending with .test.ts. Example: if the source file is named
my-stack.ts, the test file must be namedmy-stack.test.ts
Install Prompt
Add this prompt to your Amazon Q CLI prompt library:
1. Download to local prompt library:
mkdir -p .amazonq/cli-prompts && curl -o .amazonq/cli-prompts/cdk-snapshot-tests-for-typescript-and-jest.md https://promptz.dev/prompts/testing/cdk-snapshot-tests-for-typescript-and-jest/2. Use with Q CLI:
q prompts use cdk-snapshot-tests-for-typescript-and-jest