CDK Snapshot Tests for Typescript and jest

Uses the dev agent to create CDK snapshot tests following best practices and testing guidelines.

Created Jan 17, 2025 Updated Jan 17, 2025 by cremich

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 describe blocks.
  • use the test() method to run an individual test
  • do not import describe, test, jest or expect.
  • use the aws-cdk-lib.assertions library from CDK v2 for assertions
  • use single named imports
  • use arrow functions for all test functions.
  • 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 named my-stack.test.ts

Related Content

Find Prompts, Rules & Agents

Search through our comprehensive library for Amazon Q Developer