Skip to content

Commit 168b3b0

Browse files
Add tests
1 parent 04afec8 commit 168b3b0

File tree

9 files changed

+307
-2
lines changed

9 files changed

+307
-2
lines changed

test/cdk-esm.test.ts

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,27 @@ describe('cdk-esm', async () => {
3636
test('check infra', async () => {
3737
const lambdaName = await getFunctionName(
3838
folder,
39-
'FunctionNameTestTsEsModule',
39+
'FunctionNameTestTsCommonJs',
4040
);
4141
await expectInfraDeployed(lambdaName);
4242
});
4343

44+
test('call Lambda - testTsCommonJs', async () => {
45+
const lambdaName = await getFunctionName(
46+
folder,
47+
'FunctionNameTestTsCommonJs',
48+
);
49+
50+
const payload = getSamplePayload(lambdaName);
51+
const response = await callLambda(lambdaName, payload);
52+
53+
expect(response.inputEvent).toEqual(payload);
54+
expect(response.runningLocally).toEqual(!observableMode);
55+
if (observableMode) {
56+
await validateLocalResponse(lambdaName, payload);
57+
}
58+
});
59+
4460
test('call Lambda - testTsEsModule', async () => {
4561
const lambdaName = await getFunctionName(
4662
folder,
@@ -57,12 +73,76 @@ describe('cdk-esm', async () => {
5773
}
5874
});
5975

76+
test('call Lambda - testJsCommonJs', async () => {
77+
const lambdaName = await getFunctionName(
78+
folder,
79+
'FunctionNameTestJsCommonJs',
80+
);
81+
82+
const payload = getSamplePayload(lambdaName);
83+
const response = await callLambda(lambdaName, payload);
84+
85+
expect(response.runningLocally).toEqual(!observableMode);
86+
expect(response.inputEvent).toEqual(payload);
87+
if (observableMode) {
88+
await validateLocalResponse(lambdaName, payload);
89+
}
90+
});
91+
92+
test('call Lambda - testJsEsModule', async () => {
93+
const lambdaName = await getFunctionName(
94+
folder,
95+
'FunctionNameTestJsEsModule',
96+
);
97+
98+
const payload = getSamplePayload(lambdaName);
99+
const response = await callLambda(lambdaName, payload);
100+
101+
expect(response.inputEvent).toEqual(payload);
102+
expect(response.runningLocally).toEqual(!observableMode);
103+
if (observableMode) {
104+
await validateLocalResponse(lambdaName, payload);
105+
}
106+
});
107+
108+
test('call Lambda - testJsCommonJsBase', async () => {
109+
const lambdaName = await getFunctionName(
110+
folder,
111+
'FunctionNameTestJsCommonJsBase',
112+
);
113+
114+
const payload = getSamplePayload(lambdaName);
115+
const response = await callLambda(lambdaName, payload);
116+
117+
expect(response.inputEvent).toEqual(payload);
118+
expect(response.runningLocally).toEqual(!observableMode);
119+
if (observableMode) {
120+
await validateLocalResponse(lambdaName, payload);
121+
}
122+
});
123+
124+
test('call Lambda - testJsEsModuleBase', async () => {
125+
const lambdaName = await getFunctionName(
126+
folder,
127+
'FunctionNameTestJsEsModuleBase',
128+
);
129+
130+
const payload = getSamplePayload(lambdaName);
131+
const response = await callLambda(lambdaName, payload);
132+
133+
expect(response.inputEvent).toEqual(payload);
134+
expect(response.runningLocally).toEqual(!observableMode);
135+
if (observableMode) {
136+
await validateLocalResponse(lambdaName, payload);
137+
}
138+
});
139+
60140
test('remove infra', async () => {
61141
if (process.env.CI === 'true' || process.env.RUN_TEST_FROM_CLI === 'true') {
62142
await removeInfra(lldProcess, folder);
63143
const lambdaName = await getFunctionName(
64144
folder,
65-
'FunctionNameTestTsEsModule',
145+
'FunctionNameTestTsCommonJs',
66146
);
67147
await expectInfraRemoved(lambdaName);
68148
}

test/cdk-esm/lib/cdk-esm-stack.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ export class CdkEsmStack extends cdk.Stack {
1212
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
1313
super(scope, id, props);
1414

15+
const functionTestTsCommonJs = new lambda_nodejs.NodejsFunction(
16+
this,
17+
'TestTsCommonJs',
18+
{
19+
// a different way to get the path
20+
entry: path.join(__dirname, '../services/testTsCommonJs/lambda.ts'),
21+
handler: 'lambdaHandler',
22+
runtime: lambda.Runtime.NODEJS_20_X,
23+
logRetention: log.RetentionDays.ONE_DAY,
24+
},
25+
);
26+
1527
const functionTestTsEsModule = new lambda_nodejs.NodejsFunction(
1628
this,
1729
'TestTsEsModule',
@@ -38,8 +50,76 @@ export class CdkEsmStack extends cdk.Stack {
3850
},
3951
);
4052

53+
const functionTestJsCommonJs = new lambda_nodejs.NodejsFunction(
54+
this,
55+
'TestJsCommonJs',
56+
{
57+
entry: 'services/testJsCommonJs/lambda.js',
58+
handler: 'lambdaHandler',
59+
runtime: lambda.Runtime.NODEJS_20_X,
60+
logRetention: log.RetentionDays.ONE_DAY,
61+
},
62+
);
63+
64+
//testJsEsModule
65+
const functionTestJsEsModule = new lambda_nodejs.NodejsFunction(
66+
this,
67+
'TestJsEsModule',
68+
{
69+
entry: 'services/testJsEsModule/lambda.js',
70+
handler: 'lambdaHandler',
71+
runtime: lambda.Runtime.NODEJS_20_X,
72+
bundling: {
73+
format: lambda_nodejs.OutputFormat.ESM,
74+
},
75+
logRetention: log.RetentionDays.ONE_DAY,
76+
},
77+
);
78+
79+
const functionTestJsCommonJsBase = new lambda.Function(
80+
this,
81+
'TestJsCommonJsBase',
82+
{
83+
runtime: lambda.Runtime.NODEJS_20_X,
84+
handler: 'lambda.lambdaHandler',
85+
code: lambda.Code.fromAsset('services/testJsCommonJs'),
86+
logRetention: log.RetentionDays.ONE_DAY,
87+
},
88+
);
89+
90+
const functionTestJsEsModuleBase = new lambda.Function(
91+
this,
92+
'TestJsEsModuleBase',
93+
{
94+
runtime: lambda.Runtime.NODEJS_20_X,
95+
handler: 'lambda.lambdaHandler',
96+
code: lambda.Code.fromAsset('services/testJsEsModule'),
97+
logRetention: log.RetentionDays.ONE_DAY,
98+
},
99+
);
100+
101+
new cdk.CfnOutput(this, 'FunctionNameTestTsCommonJs', {
102+
value: functionTestTsCommonJs.functionName,
103+
});
104+
41105
new cdk.CfnOutput(this, 'FunctionNameTestTsEsModule', {
42106
value: functionTestTsEsModule.functionName,
43107
});
108+
109+
new cdk.CfnOutput(this, 'FunctionNameTestJsCommonJs', {
110+
value: functionTestJsCommonJs.functionName,
111+
});
112+
113+
new cdk.CfnOutput(this, 'FunctionNameTestJsEsModule', {
114+
value: functionTestJsEsModule.functionName,
115+
});
116+
117+
new cdk.CfnOutput(this, 'FunctionNameTestJsCommonJsBase', {
118+
value: functionTestJsCommonJsBase.functionName,
119+
});
120+
121+
new cdk.CfnOutput(this, 'FunctionNameTestJsEsModuleBase', {
122+
value: functionTestJsEsModuleBase.functionName,
123+
});
44124
}
45125
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const { STSClient, GetCallerIdentityCommand } = require('@aws-sdk/client-sts');
2+
3+
const stsClient = new STSClient({});
4+
5+
const lambdaHandler = async (event, context) => {
6+
// Check context
7+
const remainingTime = context.getRemainingTimeInMillis();
8+
if (remainingTime === undefined) {
9+
throw new Error('Remaining time is undefined');
10+
}
11+
12+
// check if SDK works
13+
const command = new GetCallerIdentityCommand({});
14+
const identity = await stsClient.send(command);
15+
16+
const response = {
17+
inputEvent: event,
18+
accountId: identity.Account,
19+
runningLocally: process.env.IS_LOCAL === 'true',
20+
};
21+
22+
if (process.env.IS_LOCAL === 'true') {
23+
const fs = require('fs');
24+
const path = require('path');
25+
const filePath = path.join(
26+
'..',
27+
'local_lambda_responses',
28+
`${context.functionName}.json`,
29+
);
30+
31+
fs.writeFileSync(filePath, JSON.stringify(response, null, 2));
32+
}
33+
34+
return response;
35+
};
36+
37+
// Export the lambda handler if needed, e.g., for unit testing
38+
module.exports = {
39+
lambdaHandler,
40+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "cdk-esm-test-js-commonjs",
3+
"version": "1.0.0",
4+
"type": "commonjs",
5+
"dependencies": {
6+
"@aws-sdk/client-sts": "^3.577.0"
7+
}
8+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { STSClient, GetCallerIdentityCommand } from '@aws-sdk/client-sts';
2+
3+
const stsClient = new STSClient({});
4+
5+
export const lambdaHandler = async (event, context) => {
6+
// check context
7+
const remainingTime = context.getRemainingTimeInMillis();
8+
if (remainingTime === undefined) {
9+
throw new Error('Remaining time is undefined');
10+
}
11+
12+
// check SDK works
13+
const command = new GetCallerIdentityCommand({});
14+
const identity = await stsClient.send(command);
15+
16+
const response = {
17+
inputEvent: event,
18+
accountId: identity.Account,
19+
runningLocally: process.env.IS_LOCAL === 'true',
20+
};
21+
22+
if (process.env.IS_LOCAL === 'true') {
23+
const fs = await import('fs');
24+
const path = await import('path');
25+
const filePath = path.join(
26+
'..',
27+
'local_lambda_responses',
28+
`${context.functionName}.json`,
29+
);
30+
31+
fs.writeFileSync(filePath, JSON.stringify(response, null, 2));
32+
}
33+
34+
return response;
35+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "cdk-esm-test-js-esmodule",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"dependencies": {
6+
"@aws-sdk/client-sts": "^3.577.0"
7+
}
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Handler } from 'aws-lambda';
2+
import { STSClient, GetCallerIdentityCommand } from '@aws-sdk/client-sts';
3+
4+
const stsClient = new STSClient({});
5+
6+
export const lambdaHandler: Handler = async (event, context) => {
7+
// check context
8+
const remainingTime = context.getRemainingTimeInMillis();
9+
if (remainingTime === undefined) {
10+
throw new Error('Remaining time is undefined');
11+
}
12+
13+
// check SDK works
14+
const command = new GetCallerIdentityCommand({});
15+
const identity = await stsClient.send(command);
16+
17+
const response = {
18+
inputEvent: event,
19+
accountId: identity.Account,
20+
runningLocally: process.env.IS_LOCAL === 'true',
21+
};
22+
23+
if (process.env.IS_LOCAL === 'true') {
24+
const fs = await import('fs');
25+
const path = await import('path');
26+
const filePath = path.join(
27+
'..',
28+
'local_lambda_responses',
29+
`${context.functionName}.json`,
30+
);
31+
32+
fs.writeFileSync(filePath, JSON.stringify(response, null, 2));
33+
}
34+
35+
return response;
36+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "cdk-esm-test-ts-commonjs",
3+
"version": "1.0.0",
4+
"type": "commonjs",
5+
"devDependencies": {
6+
"@tsconfig/node20": "^20.1.4"
7+
},
8+
"dependencies": {
9+
"@aws-sdk/client-sts": "^3.577.0"
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "@tsconfig/node20/tsconfig.json",
3+
"compilerOptions": {
4+
"moduleResolution": "node",
5+
"module": "CommonJS"
6+
}
7+
}

0 commit comments

Comments
 (0)