CDK Testing Rules
General
- Follow a test-driven development approach.
- Keep tests simple and focused on one aspect of behavior
- Use Jest as the testing framework
- Use the AWS CDK assertions module for testing CDK constructs
- Use the Arrange-Act-Assert pattern
- Organize tests by construct or stack
- Use descriptive test names that explain the expected behavior
- Aim for high test coverage (at least 80%)
- Focus on testing critical infrastructure components
- Ensure all resource properties are tested
Unit Tests
- Use fine-grained assertions to test specific aspects of resources
- Use
hasResourcePropertiesfor partial matching andexactlyMatchTemplatefor exact matching - Use snapshot testing to detect unintended changes in CloudFormation templates
- Update snapshots only when changes are intentional
- Don’t rely solely on snapshots; combine with fine-grained assertions
Integration Tests
- Use the
integ-tests-alphamodule for integration testing - Create separate integration test files with the
.integ.tssuffix - Define integration tests as CDK applications
- Test that resources are created successfully
- Test interactions between resources
- Clean up resources after tests complete
Install Project Rule
Add this rule to your project's context:
1. Download to project rules:
mkdir -p .amazonq/rules && curl -o .amazonq/rules/cdk-testing.md https://promptz.dev/rules/cdk/cdk-testing/index.md