Skip to content

Commit 8de034f

Browse files
committed
Add tests
1 parent dcdc0ae commit 8de034f

7 files changed

Lines changed: 195 additions & 2 deletions

File tree

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"watch": "nodemon --watch src --ext ts,js --exec 'npx esbuild-node-tsc'",
88
"lint": "eslint .",
99
"prettify": "prettier --write --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"",
10-
"release": "npm publish --access=public --tag=latest"
10+
"release": "npm publish --access=public --tag=latest",
11+
"test": "vitest"
1112
},
1213
"peerDependencies": {
1314
"aws-cdk-lib": "^2.55.0",
@@ -26,6 +27,7 @@
2627
"eslint-plugin-import": "^2.26.0",
2728
"nodemon": "^2.0.20",
2829
"prettier": "^2.7.1",
29-
"typescript": "^4.8.4"
30+
"typescript": "^4.8.4",
31+
"vitest": "^0.25.8"
3032
}
3133
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { ConsoleFunction } from '../../src/function/ConsoleFunction';
3+
import { compileTestStack } from '../helper';
4+
5+
describe('ConsoleFunction', () => {
6+
it('adds the console layer', () => {
7+
const template = compileTestStack((stack) => {
8+
new ConsoleFunction(stack, 'Console', {
9+
handler: 'index.php',
10+
});
11+
});
12+
13+
const consoleFunction = template.findResources('AWS::Lambda::Function');
14+
const layers = consoleFunction.Console63CA37A7.Properties.Layers;
15+
expect(layers).length(2);
16+
expect(layers[0]).to.match(/arn:aws:lambda:us-east-1:534081306603:layer:php-81:\d+/);
17+
expect(layers[1]).to.match(/arn:aws:lambda:us-east-1:534081306603:layer:console:\d+/);
18+
});
19+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { PhpFpmFunction, PhpFunction } from '../../src';
3+
import { compileTestStack } from '../helper';
4+
5+
describe('PhpFpmFunction', () => {
6+
it('builds', () => {
7+
const template = compileTestStack((stack) => {
8+
new PhpFpmFunction(stack, 'Function', {
9+
handler: 'index.php',
10+
});
11+
}).toJSON();
12+
// Remove random data that changes every time
13+
delete template.Resources.Function76856677?.Properties?.Code?.S3Key;
14+
15+
expect(template.Resources).toMatchSnapshot();
16+
});
17+
});

test/function/PhpFunction.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { PhpFunction } from '../../src';
3+
import { compileTestStack } from '../helper';
4+
5+
describe('PhpFunction', () => {
6+
it('builds', () => {
7+
const template = compileTestStack((stack) => {
8+
new PhpFunction(stack, 'Function', {
9+
handler: 'index.php',
10+
});
11+
}).toJSON();
12+
// Remove random data that changes every time
13+
delete template.Resources.Function76856677?.Properties?.Code?.S3Key;
14+
15+
expect(template.Resources).toMatchSnapshot();
16+
});
17+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Vitest Snapshot v1
2+
3+
exports[`PhpFpmFunction > builds 1`] = `
4+
{
5+
"Function76856677": {
6+
"DependsOn": [
7+
"FunctionServiceRole675BB04A",
8+
],
9+
"Properties": {
10+
"Code": {
11+
"S3Bucket": {
12+
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-us-east-1",
13+
},
14+
},
15+
"Description": "HTTP application",
16+
"Handler": "index.php",
17+
"Layers": [
18+
"arn:aws:lambda:us-east-1:534081306603:layer:php-81-fpm:2",
19+
],
20+
"MemorySize": 1024,
21+
"Role": {
22+
"Fn::GetAtt": [
23+
"FunctionServiceRole675BB04A",
24+
"Arn",
25+
],
26+
},
27+
"Runtime": "provided.al2",
28+
"Timeout": 28,
29+
},
30+
"Type": "AWS::Lambda::Function",
31+
},
32+
"FunctionServiceRole675BB04A": {
33+
"Properties": {
34+
"AssumeRolePolicyDocument": {
35+
"Statement": [
36+
{
37+
"Action": "sts:AssumeRole",
38+
"Effect": "Allow",
39+
"Principal": {
40+
"Service": "lambda.amazonaws.com",
41+
},
42+
},
43+
],
44+
"Version": "2012-10-17",
45+
},
46+
"ManagedPolicyArns": [
47+
{
48+
"Fn::Join": [
49+
"",
50+
[
51+
"arn:",
52+
{
53+
"Ref": "AWS::Partition",
54+
},
55+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
56+
],
57+
],
58+
},
59+
],
60+
},
61+
"Type": "AWS::IAM::Role",
62+
},
63+
}
64+
`;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Vitest Snapshot v1
2+
3+
exports[`PhpFunction > builds 1`] = `
4+
{
5+
"Function76856677": {
6+
"DependsOn": [
7+
"FunctionServiceRole675BB04A",
8+
],
9+
"Properties": {
10+
"Code": {
11+
"S3Bucket": {
12+
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-us-east-1",
13+
},
14+
},
15+
"Handler": "index.php",
16+
"Layers": [
17+
"arn:aws:lambda:us-east-1:534081306603:layer:php-81:2",
18+
],
19+
"MemorySize": 1024,
20+
"Role": {
21+
"Fn::GetAtt": [
22+
"FunctionServiceRole675BB04A",
23+
"Arn",
24+
],
25+
},
26+
"Runtime": "provided.al2",
27+
"Timeout": 6,
28+
},
29+
"Type": "AWS::Lambda::Function",
30+
},
31+
"FunctionServiceRole675BB04A": {
32+
"Properties": {
33+
"AssumeRolePolicyDocument": {
34+
"Statement": [
35+
{
36+
"Action": "sts:AssumeRole",
37+
"Effect": "Allow",
38+
"Principal": {
39+
"Service": "lambda.amazonaws.com",
40+
},
41+
},
42+
],
43+
"Version": "2012-10-17",
44+
},
45+
"ManagedPolicyArns": [
46+
{
47+
"Fn::Join": [
48+
"",
49+
[
50+
"arn:",
51+
{
52+
"Ref": "AWS::Partition",
53+
},
54+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
55+
],
56+
],
57+
},
58+
],
59+
},
60+
"Type": "AWS::IAM::Role",
61+
},
62+
}
63+
`;

test/helper.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Stack } from 'aws-cdk-lib';
2+
import { Template } from 'aws-cdk-lib/assertions';
3+
4+
export function compileTestStack(definition: (stack: Stack) => void): Template {
5+
const stack = new Stack(undefined, 'app', {
6+
env: { region: 'us-east-1' },
7+
});
8+
definition(stack);
9+
10+
return Template.fromStack(stack);
11+
}

0 commit comments

Comments
 (0)