Skip to content

Commit 96d353c

Browse files
author
Michael Kaiser
committed
Fix snapshot tests by normalizing asset hashes
1 parent d68eee0 commit 96d353c

20 files changed

+153
-77
lines changed
Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,19 @@
11
import { App } from "aws-cdk-lib";
22
import { GatewayLambdaAuth } from "../../lib/stack/gateway-lambda-auth-stack";
33
import { Template } from "aws-cdk-lib/assertions";
4-
5-
/**
6-
* Normalizes asset hashes in CloudFormation templates to ensure consistent snapshots
7-
* across different environments (local vs CI/CD).
8-
*/
9-
function normalizeAssetHashes(template: any): any {
10-
// Create a deep copy of the template to avoid modifying the original
11-
const templateCopy = JSON.parse(JSON.stringify(template));
12-
13-
// Function to recursively traverse the template and replace asset hashes
14-
function replaceAssetHashes(obj: any) {
15-
if (!obj || typeof obj !== 'object') return;
16-
17-
// Process object properties
18-
for (const key in obj) {
19-
if (key === 'S3Key' && typeof obj[key] === 'string' && /^[a-f0-9]{64}\.zip$/.test(obj[key])) {
20-
// Replace asset hash with a constant placeholder
21-
obj[key] = 'NORMALIZED_ASSET_HASH.zip';
22-
} else if (typeof obj[key] === 'object') {
23-
// Recursively process nested objects and arrays
24-
replaceAssetHashes(obj[key]);
25-
}
26-
}
27-
}
28-
29-
replaceAssetHashes(templateCopy);
30-
return templateCopy;
31-
}
4+
import { normalizeTemplate } from "../../../test-utils/normalize-template";
325

336
describe('Snapshot', () => {
7+
348
it('Stack', () => {
359
const app = new App();
3610
const stack = new GatewayLambdaAuth(app, 'test-api-gateway-lambda-auth');
3711
const template = Template.fromStack(stack);
3812

39-
// Normalize asset hashes before snapshot comparison
40-
const normalizedTemplate = normalizeAssetHashes(template.toJSON());
13+
// Normalize the template before snapshot comparison
14+
const normalizedTemplate = normalizeTemplate(template.toJSON());
4115
expect(normalizedTemplate).toMatchSnapshot();
4216
});
17+
4318
})
4419

typescript/aws-transfer-sftp-server/jest.config.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

typescript/aws-transfer-sftp-server/test/aws-transfer-sftp-server.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
} from 'aws-cdk-lib';
55
import { Template } from 'aws-cdk-lib/assertions';
66
import { SftpServerStack } from '../aws-transfer-sftp-server';
7+
import { normalizeTemplate } from '../../test-utils/normalize-template';
78

89
test('SftpServerStack has required resources', () => {
910
const app = new cdk.App();
@@ -23,5 +24,8 @@ test('SftpServerStack has required resources', () => {
2324
dataBucket: bucket,
2425
});
2526

26-
expect(Template.fromStack(stack)).toMatchSnapshot();
27+
// Normalize the template before snapshot comparison
28+
const template = Template.fromStack(stack);
29+
const normalizedTemplate = normalizeTemplate(template.toJSON());
30+
expect(normalizedTemplate).toMatchSnapshot();
2731
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
roots: ['<rootDir>/test'],
4+
testMatch: ['**/*.test.ts'],
5+
transform: {
6+
'^.+\\.tsx?$': 'ts-jest'
7+
}
8+
};
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { Template } from 'aws-cdk-lib/assertions';
22
import { IntegTesting } from '../src/integ.default';
3+
import { normalizeTemplate } from '../../test-utils/normalize-template';
34

45
test('default validation', () => {
56
const integ = new IntegTesting();
67
integ.stack.forEach(stack => {
78
const t = Template.fromStack(stack);
9+
// Normalize the template before snapshot comparison
10+
const normalizedTemplate = normalizeTemplate(t.toJSON());
811
// should match snapshot
9-
expect(t).toMatchSnapshot();
12+
expect(normalizedTemplate).toMatchSnapshot();
1013
});
1114
});
Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,24 @@
11
{
22
"compilerOptions": {
3-
"outDir": "lib",
4-
"rootDir": "src",
5-
"declarationMap": false,
6-
"inlineSourceMap": true,
7-
"inlineSources": true,
8-
"alwaysStrict": true,
3+
"target": "ES2018",
4+
"module": "commonjs",
5+
"lib": ["es2018"],
96
"declaration": true,
10-
"experimentalDecorators": true,
11-
"incremental": true,
12-
"lib": [
13-
"es2020"
14-
],
15-
"module": "CommonJS",
16-
"noEmitOnError": true,
17-
"noFallthroughCasesInSwitch": true,
18-
"noImplicitAny": true,
19-
"noImplicitReturns": true,
20-
"noImplicitThis": true,
21-
"noUnusedLocals": true,
22-
"noUnusedParameters": true,
23-
"resolveJsonModule": true,
24-
"skipLibCheck": true,
257
"strict": true,
8+
"noImplicitAny": true,
269
"strictNullChecks": true,
27-
"strictPropertyInitialization": true,
28-
"stripInternal": false,
29-
"target": "ES2020",
30-
"composite": false,
31-
"tsBuildInfoFile": "lib/tsconfig.tsbuildinfo"
10+
"noImplicitThis": true,
11+
"alwaysStrict": true,
12+
"noUnusedLocals": false,
13+
"noUnusedParameters": false,
14+
"noImplicitReturns": true,
15+
"noFallthroughCasesInSwitch": false,
16+
"inlineSourceMap": true,
17+
"inlineSources": true,
18+
"experimentalDecorators": true,
19+
"strictPropertyInitialization": false,
20+
"typeRoots": ["./node_modules/@types"],
21+
"outDir": "dist"
3222
},
33-
"include": [
34-
"src/**/*.ts"
35-
],
36-
"exclude": [
37-
"node_modules"
38-
]
23+
"exclude": ["node_modules", "cdk.out"]
3924
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
roots: ['<rootDir>/test'],
4+
testMatch: ['**/*.test.ts'],
5+
transform: {
6+
'^.+\\.tsx?$': 'ts-jest'
7+
}
8+
};

typescript/ec2-instance/test/main.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { App, Stack } from 'aws-cdk-lib';
22
import { Template } from 'aws-cdk-lib/assertions';
33
import { EC2Example } from '../src/ec2-instance';
44
import { InstanceSize, CPUTypes } from '../src/envValidator';
5+
import { normalizeTemplate } from '../../test-utils/normalize-template';
56

67
const devEnv = {
78
account: process.env.CDK_DEFAULT_ACCOUNT,
@@ -28,7 +29,8 @@ test('Snapshot', () => {
2829
});
2930

3031
const template = Template.fromStack(stack);
31-
expect(template.toJSON()).toMatchSnapshot();
32+
const normalizedTemplate = normalizeTemplate(template.toJSON());
33+
expect(normalizedTemplate).toMatchSnapshot();
3234
});
3335

3436
test('LARGE', () => {

typescript/ec2-ssm-local-zone/jest.config.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import * as cdk from 'aws-cdk-lib';
22
import { Template } from 'aws-cdk-lib/assertions';
33
import { ExampleLocalZoneStack } from '../example-localzone-stack';
4+
import { normalizeTemplate } from '../../test-utils/normalize-template';
45

56
test('ExampleLocalZoneStack has required resources', () => {
67
const app = new cdk.App();
78
// WHEN
89
const stack = new ExampleLocalZoneStack(app, 'ExampleLocalZoneStack', { });
910

10-
expect(Template.fromStack(stack)).toMatchSnapshot();
11+
const template = Template.fromStack(stack);
12+
const normalizedTemplate = normalizeTemplate(template.toJSON());
13+
expect(normalizedTemplate).toMatchSnapshot();
1114
});

0 commit comments

Comments
 (0)