Skip to content

Commit 3427490

Browse files
committed
Make tests more stable
1 parent b05fe22 commit 3427490

6 files changed

Lines changed: 37 additions & 12 deletions

File tree

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
"constructs": "^10.0.0"
1414
},
1515
"dependencies": {
16-
"@bref.sh/layers": "^2.0.3"
16+
"@bref.sh/layers": "^2.0.21"
1717
},
1818
"devDependencies": {
19+
"@types/lodash": "^4.14.191",
1920
"@types/node": "^18.11.3",
2021
"@typescript-eslint/eslint-plugin": "^5.40.1",
2122
"@typescript-eslint/parser": "^5.40.1",
@@ -25,6 +26,7 @@
2526
"eslint": "8.22.0",
2627
"eslint-config-prettier": "^8.5.0",
2728
"eslint-plugin-import": "^2.26.0",
29+
"lodash": "^4.17.21",
2830
"nodemon": "^2.0.20",
2931
"prettier": "^2.7.1",
3032
"typescript": "^4.8.4",
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest';
2-
import { PhpFpmFunction, PhpFunction } from '../../src';
3-
import { compileTestStack } from '../helper';
2+
import { PhpFpmFunction } from '../../src';
3+
import { cleanupTemplate, compileTestStack } from '../helper';
44

55
describe('PhpFpmFunction', () => {
66
it('builds', () => {
@@ -9,9 +9,7 @@ describe('PhpFpmFunction', () => {
99
handler: 'index.php',
1010
});
1111
}).toJSON();
12-
// Remove random data that changes every time
13-
delete template.Resources.Function76856677?.Properties?.Code?.S3Key;
1412

15-
expect(template.Resources).toMatchSnapshot();
13+
expect(cleanupTemplate(template).Resources).toMatchSnapshot();
1614
});
1715
});

test/function/PhpFunction.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest';
22
import { PhpFunction } from '../../src';
3-
import { compileTestStack } from '../helper';
3+
import { cleanupTemplate, compileTestStack } from '../helper';
44

55
describe('PhpFunction', () => {
66
it('builds', () => {
@@ -9,9 +9,7 @@ describe('PhpFunction', () => {
99
handler: 'index.php',
1010
});
1111
}).toJSON();
12-
// Remove random data that changes every time
13-
delete template.Resources.Function76856677?.Properties?.Code?.S3Key;
1412

15-
expect(template.Resources).toMatchSnapshot();
13+
expect(cleanupTemplate(template).Resources).toMatchSnapshot();
1614
});
1715
});

test/function/__snapshots__/PhpFpmFunction.test.ts.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ exports[`PhpFpmFunction > builds 1`] = `
1111
"S3Bucket": {
1212
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-us-east-1",
1313
},
14+
"S3Key": "<removed>",
1415
},
1516
"Description": "HTTP application",
1617
"Handler": "index.php",
1718
"Layers": [
18-
"arn:aws:lambda:us-east-1:534081306603:layer:php-81-fpm:2",
19+
"arn:aws:lambda:us-east-1:534081306603:layer:php-81-fpm:<removed>",
1920
],
2021
"MemorySize": 1024,
2122
"Role": {

test/function/__snapshots__/PhpFunction.test.ts.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ exports[`PhpFunction > builds 1`] = `
1111
"S3Bucket": {
1212
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-us-east-1",
1313
},
14+
"S3Key": "<removed>",
1415
},
1516
"Handler": "index.php",
1617
"Layers": [
17-
"arn:aws:lambda:us-east-1:534081306603:layer:php-81:2",
18+
"arn:aws:lambda:us-east-1:534081306603:layer:php-81:<removed>",
1819
],
1920
"MemorySize": 1024,
2021
"Role": {

test/helper.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Stack } from 'aws-cdk-lib';
22
import { Template } from 'aws-cdk-lib/assertions';
3+
import { mapValues } from 'lodash';
34

45
export function compileTestStack(definition: (stack: Stack) => void): Template {
56
const stack = new Stack(undefined, 'app', {
@@ -9,3 +10,27 @@ export function compileTestStack(definition: (stack: Stack) => void): Template {
910

1011
return Template.fromStack(stack);
1112
}
13+
14+
type CompiledTemplate = { [key: string]: any };
15+
16+
export function cleanupTemplate(template: CompiledTemplate): CompiledTemplate {
17+
template.Resources = mapValues(template.Resources, (resource) => {
18+
if (resource.Type === 'AWS::Lambda::Function') {
19+
if (resource.Properties.Code.S3Key) {
20+
resource.Properties.Code.S3Key = '<removed>';
21+
}
22+
if (Array.isArray(resource.Properties.Layers)) {
23+
resource.Properties.Layers = resource.Properties.Layers.map((layer: string) =>
24+
layer.replace(
25+
/(arn:aws:lambda:[a-z0-9\-]+:534081306603:layer:[a-z0-9\-]+:)\d+/,
26+
'$1<removed>'
27+
)
28+
);
29+
}
30+
}
31+
32+
return resource;
33+
});
34+
35+
return template;
36+
}

0 commit comments

Comments
 (0)