Skip to content

Commit c8dd8ed

Browse files
author
Michael Kaiser
committed
Add documentation for snapshot testing with asset hash normalization
1 parent 96d353c commit c8dd8ed

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

SNAPSHOT_TESTING.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# CDK Snapshot Testing Guide
2+
3+
This guide explains how to use the asset hash normalization utility for consistent CDK snapshot testing across different environments.
4+
5+
## Problem
6+
7+
CDK snapshot tests often fail in CI/CD environments because asset hashes change between different environments. This happens because:
8+
9+
- Asset hashes are generated based on content and environment variables
10+
- Different machines or CI/CD environments produce different hashes
11+
- This causes snapshot tests to fail even when there are no actual changes to the infrastructure
12+
13+
## Solution
14+
15+
We've created a utility function that normalizes asset hashes and other environment-specific values in CloudFormation templates before comparing them with snapshots.
16+
17+
## How to Use the Normalization Utility
18+
19+
### 1. Import the Utility
20+
21+
```typescript
22+
import { normalizeTemplate } from '../../test-utils/normalize-template';
23+
```
24+
25+
### 2. Apply Normalization Before Snapshot Comparison
26+
27+
```typescript
28+
import { App } from 'aws-cdk-lib';
29+
import { Template } from 'aws-cdk-lib/assertions';
30+
import { YourStack } from '../lib/your-stack';
31+
import { normalizeTemplate } from '../../test-utils/normalize-template';
32+
33+
test('YourStack creates the expected resources', () => {
34+
const app = new App();
35+
const stack = new YourStack(app, 'TestStack');
36+
37+
// Get the CloudFormation template
38+
const template = Template.fromStack(stack);
39+
40+
// Normalize the template before snapshot comparison
41+
const normalizedTemplate = normalizeTemplate(template.toJSON());
42+
43+
// Compare with snapshot
44+
expect(normalizedTemplate).toMatchSnapshot();
45+
});
46+
```
47+
48+
### 3. Update Existing Snapshots
49+
50+
After adding the normalization to your tests, update your snapshots:
51+
52+
```bash
53+
npm test -- -u
54+
```
55+
56+
## What Gets Normalized
57+
58+
The utility currently normalizes:
59+
60+
1. **S3 Asset Keys**: Replaces asset hashes in S3Key properties
61+
- Pattern with extension: `[64 hex chars].zip``NORMALIZED_ASSET_HASH.zip`
62+
- Pattern without extension: `[64 hex chars]``NORMALIZED_ASSET_HASH`
63+
64+
2. **Docker Image Digests**: Replaces image digests
65+
- Pattern: `sha256:[digest]``NORMALIZED_IMAGE_DIGEST`
66+
67+
## Adding New Test Files
68+
69+
When creating new test files that use snapshot testing:
70+
71+
1. Import the normalization utility
72+
2. Apply it to your template before comparing with snapshots
73+
3. Update your snapshots with the `-u` flag
74+
75+
## Extending the Utility
76+
77+
If you encounter other environment-specific values that need normalization, you can extend the utility at `typescript/test-utils/normalize-template.ts`.
78+
79+
Example of adding a new normalization rule:
80+
81+
```typescript
82+
// Inside the normalizeValues function
83+
if (key === 'NewPropertyToNormalize' && typeof obj[key] === 'string' && /pattern-to-match/.test(obj[key])) {
84+
obj[key] = 'NORMALIZED_VALUE';
85+
}
86+
```
87+
88+
## Troubleshooting
89+
90+
If you're still seeing snapshot test failures:
91+
92+
1. **Check for new patterns**: There might be new types of asset hashes or environment-specific values that need normalization
93+
2. **Verify imports**: Make sure you're importing and using the utility correctly
94+
3. **Check snapshot updates**: Ensure you've updated your snapshots after adding normalization
95+
96+
## Best Practices
97+
98+
1. **Always normalize before snapshot comparison**: This ensures consistent results
99+
2. **Update snapshots deliberately**: Only use the `-u` flag when you expect changes
100+
3. **Review snapshot diffs**: When updating snapshots, review the changes to ensure they're expected
101+
4. **Keep the utility updated**: As new patterns emerge, add them to the normalization utility
102+
103+
## Additional Resources
104+
105+
- [Jest Snapshot Testing Documentation](https://jestjs.io/docs/snapshot-testing)
106+
- [AWS CDK Testing Documentation](https://docs.aws.amazon.com/cdk/v2/guide/testing.html)

0 commit comments

Comments
 (0)