Skip to content

Commit 96da83c

Browse files
chore: add tests
1 parent aa25ca5 commit 96da83c

File tree

18 files changed

+397
-5
lines changed

18 files changed

+397
-5
lines changed

.vscode/launch.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,46 @@
150150
"type": "node",
151151
"cwd": "${workspaceRoot}/test/osls-basic"
152152
},
153+
{
154+
"name": "LLDebugger - OSLS EsBuild CJS",
155+
"program": "${workspaceRoot}/node_modules/tsx/dist/cli.mjs",
156+
"args": ["../../src/lldebugger.ts"],
157+
"request": "launch",
158+
"skipFiles": ["<node_internals>/**"],
159+
"console": "integratedTerminal",
160+
"type": "node",
161+
"cwd": "${workspaceRoot}/test/osls-esbuild-cjs"
162+
},
163+
{
164+
"name": "LLDebugger - OSLS EsBuild CJS - observability",
165+
"program": "${workspaceRoot}/node_modules/tsx/dist/cli.mjs",
166+
"args": ["../../src/lldebugger.ts", "-o"],
167+
"request": "launch",
168+
"skipFiles": ["<node_internals>/**"],
169+
"console": "integratedTerminal",
170+
"type": "node",
171+
"cwd": "${workspaceRoot}/test/osls-esbuild-cjs"
172+
},
173+
{
174+
"name": "LLDebugger - OSLS EsBuild ESM",
175+
"program": "${workspaceRoot}/node_modules/tsx/dist/cli.mjs",
176+
"args": ["../../src/lldebugger.ts"],
177+
"request": "launch",
178+
"skipFiles": ["<node_internals>/**"],
179+
"console": "integratedTerminal",
180+
"type": "node",
181+
"cwd": "${workspaceRoot}/test/osls-esbuild-esm"
182+
},
183+
{
184+
"name": "LLDebugger - OSLS EsBuild ESM - observability",
185+
"program": "${workspaceRoot}/node_modules/tsx/dist/cli.mjs",
186+
"args": ["../../src/lldebugger.ts", "-o"],
187+
"request": "launch",
188+
"skipFiles": ["<node_internals>/**"],
189+
"console": "integratedTerminal",
190+
"type": "node",
191+
"cwd": "${workspaceRoot}/test/osls-esbuild-esm"
192+
},
153193
{
154194
"name": "LLDebugger - SAM basic",
155195
"program": "${workspaceRoot}/node_modules/tsx/dist/cli.mjs",

package-lock.json

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

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
"test-sls-esbuild-esm-observable": "npm run build && RUN_TEST_FROM_CLI=true OBSERVABLE_MODE=true vitest run test/sls-esbuild-esm.test.ts",
6363
"test-osls-basic": "npm run build && RUN_TEST_FROM_CLI=true vitest run test/osls-basic.test.ts",
6464
"test-osls-basic-observable": "npm run build && RUN_TEST_FROM_CLI=true OBSERVABLE_MODE=true vitest run test/osls-basic.test.ts",
65+
"test-osls-esbuild-cjs": "npm run build && RUN_TEST_FROM_CLI=true vitest run test/osls-esbuild-cjs.test.ts",
66+
"test-osls-esbuild-cjs-observable": "npm run build && RUN_TEST_FROM_CLI=true OBSERVABLE_MODE=true vitest run test/osls-esbuild-cjs.test.ts",
67+
"test-osls-esbuild-esm": "npm run build && RUN_TEST_FROM_CLI=true vitest run test/osls-esbuild-esm.test.ts",
68+
"test-osls-esbuild-esm-observable": "npm run build && RUN_TEST_FROM_CLI=true OBSERVABLE_MODE=true vitest run test/osls-esbuild-esm.test.ts",
6569
"test-sam-basic": "npm run build && RUN_TEST_FROM_CLI=true vitest run test/sam-basic.test.ts",
6670
"test-sam-basic-observable": "npm run build && RUN_TEST_FROM_CLI=true OBSERVABLE_MODE=true vitest run test/sam-basic.test.ts",
6771
"test-sam-alt": "npm run build && RUN_TEST_FROM_CLI=true vitest run test/sam-alt.test.ts",
@@ -145,8 +149,10 @@
145149
"test/sls-basic",
146150
"test/sls-esbuild",
147151
"test/osls-basic",
152+
"test/osls-esbuild",
153+
"test/sls-esbuild-cjs",
148154
"test/sam-basic",
149155
"test/sam-alt",
150156
"test/terraform-basic"
151157
]
152-
}
158+
}

test/osls-esbuild-cjs.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { expect, test, describe, beforeAll, afterAll } from 'vitest';
2+
import { ChildProcess } from 'child_process';
3+
import { startDebugger } from './utils/startDebugger.js';
4+
import { expectInfraRemoved } from './utils/expectInfraRemoved.js';
5+
import { expectInfraDeployed } from './utils/expectInfraDeployed.js';
6+
import { removeInfra } from './utils/removeInfra.js';
7+
import { exec } from 'child_process';
8+
import { promisify } from 'util';
9+
import { callLambda } from './utils/callLambda.js';
10+
import { getSamplePayload } from './utils/getSamplePayload.js';
11+
import { validateLocalResponse } from './utils/validateLocalResponse.js';
12+
import { getTestProjectFolder } from './utils/getTestProjectFolder.js';
13+
14+
export const execAsync = promisify(exec);
15+
16+
const observableMode = process.env.OBSERVABLE_MODE === 'true';
17+
18+
describe('osls-esbuild-cjs', async () => {
19+
const folder = await getTestProjectFolder('osls-esbuild-cjs');
20+
let lldProcess: ChildProcess | undefined;
21+
22+
beforeAll(async () => {
23+
if (process.env.CI === 'true' || process.env.RUN_TEST_FROM_CLI === 'true') {
24+
lldProcess = await startDebugger(folder);
25+
}
26+
});
27+
28+
afterAll(async () => {
29+
// stop the debugger
30+
lldProcess?.kill();
31+
});
32+
33+
test('check infra', async () => {
34+
const lambdaName = 'lls-osls-esbuild-cjs-test-testTsCommonJs';
35+
await expectInfraDeployed(lambdaName);
36+
});
37+
38+
test('call Lambda - testTsCommonJs', async () => {
39+
const lambdaName = 'lls-osls-esbuild-cjs-test-testTsCommonJs';
40+
41+
const payload = getSamplePayload(lambdaName);
42+
const response = await callLambda(lambdaName, payload);
43+
44+
expect(response.inputEvent).toEqual(payload);
45+
expect(response.runningLocally).toEqual(!observableMode);
46+
if (observableMode) {
47+
await validateLocalResponse(lambdaName, payload);
48+
}
49+
});
50+
51+
test('remove infra', async () => {
52+
if (process.env.CI === 'true' || process.env.RUN_TEST_FROM_CLI === 'true') {
53+
await removeInfra(lldProcess, folder);
54+
const lambdaName = 'lls-osls-esbuild-cjs-test-testTsCommonJs';
55+
await expectInfraRemoved(lambdaName);
56+
}
57+
});
58+
});

test/osls-esbuild-cjs/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# package directories
2+
node_modules
3+
jspm_packages
4+
5+
# Serverless directories
6+
.serverless

test/osls-esbuild-cjs/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "osls-esbuild",
3+
"version": "1.0.0",
4+
"description": "",
5+
"type": "module",
6+
"main": "index.js",
7+
"scripts": {
8+
"deploy": "serverless deploy",
9+
"destroy": "serverless remove"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC",
14+
"devDependencies": {
15+
"esbuild": "^0.20.2",
16+
"osls": "^3.41.0",
17+
"serverless-esbuild": "^1.52.1",
18+
"@tsconfig/node20": "^20.1.4"
19+
},
20+
"dependencies": {
21+
"@aws-sdk/client-sts": "^3.577.0"
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
service: lls-osls-esbuild-cjs
2+
frameworkVersion: '3'
3+
4+
plugins:
5+
- serverless-esbuild
6+
7+
provider:
8+
name: aws
9+
runtime: nodejs20.x
10+
region: eu-west-1
11+
stage: test
12+
13+
custom:
14+
esbuild:
15+
bundle: true
16+
minify: false
17+
18+
functions:
19+
testTsCommonJs:
20+
handler: services/testTsCommonJs/lambda.lambdaHandler
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "sls-esbuild-test-ts-commonjs",
3+
"version": "1.0.0",
4+
"type": "commonjs",
5+
"dependencies": {
6+
"@aws-sdk/client-sts": "^3.577.0"
7+
}
8+
}
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)