Skip to content

Commit 98afd9b

Browse files
authored
fix(testing-sdk): fix errors when running as esm (#390)
*Issue #, if available:* #387 *Description of changes:* The testing SDK does not work in ESM due to a dependency on `__dirname` which is CJS-only. Solution: - Adding ESM shims to the rollup config with `@rollup/plugin-esm-shim`. This adds `__dirname`, `__filename`, and `require` as global variables in our bundle (only `__dirname` is used currently). - Fix the path to the worker file when running as ESM Additional changes: - Remove file system checking and use compile-time variables to determine worker path - Add step and wait example running as ESM (in local mode using vitest, not yet running against the cloud) By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 2af8afe commit 98afd9b

File tree

12 files changed

+849
-144
lines changed

12 files changed

+849
-144
lines changed

package-lock.json

Lines changed: 597 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@eslint/compat": "^1.3.0",
3535
"@eslint/js": "^9.29.0",
3636
"@microsoft/api-documenter": "^7.28.1",
37+
"@rollup/plugin-esm-shim": "^0.1.8",
3738
"@rollup/plugin-json": "^6.1.0",
3839
"@rollup/plugin-typescript": "^12.1.4",
3940
"@tsconfig/node20": "^20.1.5",

packages/aws-durable-execution-sdk-js-examples/package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
"types": "./dist/examples/*"
1717
},
1818
"./catalog": {
19+
"types": "./dist/utils/examples-catalog.d.ts",
1920
"require": "./dist/examples-catalog.js",
20-
"import": "./dist/examples-catalog.js",
21-
"types": "./dist/utils/examples-catalog.d.ts"
21+
"import": "./dist/examples-catalog.js"
2222
}
2323
},
2424
"scripts": {
@@ -37,13 +37,11 @@
3737
"copy-source": "mkdir -p build/source && cp -r src/examples/*.ts build/source/",
3838
"copy-catalog": "cp examples-catalog.json build/aws-durable-execution-sdk-js-examples/",
3939
"generate-sam-template": "tsx scripts/generate-sam-template.ts",
40-
"test": "npm run unit-test",
40+
"test": "jest && vitest run",
4141
"test:integration": "NODE_ENV=integration jest --config jest.config.integration.js",
4242
"pretest-with-sdk-coverage": "node scripts/copy-sdk-source.js",
4343
"test-with-sdk-coverage": "jest --config jest.config.sdk-coverage.js --collectCoverage",
4444
"coverage-to-csv": "node scripts/coverage-to-csv.js",
45-
"unit-test": "jest --collectCoverage",
46-
"unit-test-watch": "jest --watch",
4745
"prettier": "prettier --write './src/**/*.*' && prettier --write './test/**/*.*'"
4846
},
4947
"dependencies": {
@@ -74,7 +72,8 @@
7472
"ts-node": "^10.9.2",
7573
"tsx": "^4.20.6",
7674
"typescript": "^5.8.2",
77-
"typescript-eslint": "^8.29.0"
75+
"typescript-eslint": "^8.29.0",
76+
"vitest": "^4.0.15"
7877
},
7978
"private": true
8079
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
OperationType,
3+
OperationStatus,
4+
LocalDurableTestRunner,
5+
} from "@aws/durable-execution-sdk-js-testing";
6+
import { handler } from "./step-basic";
7+
import { beforeAll, afterAll, describe, expect, it } from "vitest";
8+
9+
beforeAll(() => LocalDurableTestRunner.setupTestEnvironment());
10+
afterAll(() => LocalDurableTestRunner.teardownTestEnvironment());
11+
12+
describe("step-basic", () => {
13+
it("should execute step and return correct result with detailed verification", async () => {
14+
const runner = new LocalDurableTestRunner({
15+
handlerFunction: handler,
16+
});
17+
18+
const execution = await runner.run();
19+
20+
// Get step operation
21+
const stepOperation = runner.getOperationByIndex(0);
22+
23+
expect(execution.getOperations()).toHaveLength(1);
24+
expect(execution.getResult()).toStrictEqual("step completed");
25+
26+
// Verify operation details
27+
expect(stepOperation.getType()).toBe(OperationType.STEP);
28+
expect(stepOperation.getStatus()).toBe(OperationStatus.SUCCEEDED);
29+
expect(stepOperation.getStepDetails()).toBeDefined();
30+
expect(stepOperation.getStepDetails()?.result).toEqual("step completed");
31+
});
32+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {
2+
DurableContext,
3+
withDurableExecution,
4+
} from "@aws/durable-execution-sdk-js";
5+
import { ExampleConfig } from "../../../types";
6+
7+
export const config: ExampleConfig = {
8+
name: "Basic Step",
9+
description: "Basic usage of context.step() to checkpoint a simple operation",
10+
};
11+
12+
export const handler = withDurableExecution(
13+
async (event: any, context: DurableContext) => {
14+
const result = await context.step(async () => {
15+
return "step completed";
16+
});
17+
return result;
18+
},
19+
);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {
2+
OperationType,
3+
OperationStatus,
4+
LocalDurableTestRunner,
5+
} from "@aws/durable-execution-sdk-js-testing";
6+
import { handler } from "./wait";
7+
import { beforeAll, afterAll, describe, expect, it } from "vitest";
8+
9+
beforeAll(() =>
10+
LocalDurableTestRunner.setupTestEnvironment({
11+
skipTime: true,
12+
}),
13+
);
14+
afterAll(() => LocalDurableTestRunner.teardownTestEnvironment());
15+
16+
describe("wait", () => {
17+
it("should call wait for 2 seconds with comprehensive verification", async () => {
18+
const runner = new LocalDurableTestRunner({
19+
handlerFunction: handler,
20+
});
21+
22+
const execution = await runner.run();
23+
24+
// Get operation using runner helper
25+
const waitOperation = runner.getOperationByIndex(0);
26+
27+
// Verify final result
28+
expect(execution.getResult()).toBe("Function Completed");
29+
30+
// Verify operations were tracked
31+
const completedOperations = execution.getOperations();
32+
expect(completedOperations.length).toEqual(1);
33+
34+
// Verify operation data can be accessed
35+
expect(waitOperation.getType()).toBe(OperationType.WAIT);
36+
expect(waitOperation.getStatus()).toBe(OperationStatus.SUCCEEDED);
37+
expect(waitOperation.getWaitDetails()?.waitSeconds).toBe(2);
38+
expect(
39+
waitOperation.getWaitDetails()?.scheduledEndTimestamp,
40+
).toBeInstanceOf(Date);
41+
});
42+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
DurableContext,
3+
withDurableExecution,
4+
} from "@aws/durable-execution-sdk-js";
5+
import { ExampleConfig } from "../../../types";
6+
import { log } from "../../../utils/logger";
7+
8+
export const config: ExampleConfig = {
9+
name: "Wait State",
10+
description: "Basic usage of context.wait() to pause execution",
11+
};
12+
13+
export const handler = withDurableExecution(
14+
async (event: any, context: DurableContext) => {
15+
log("Hello world before wait!");
16+
await context.wait({ seconds: 2 });
17+
log("Hello world after wait!");
18+
return "Function Completed";
19+
},
20+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
include: ["src/esm-examples/**/*.test.ts"],
6+
},
7+
});

packages/aws-durable-execution-sdk-js-testing/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"devDependencies": {
4646
"@eslint/compat": "^1.3.0",
4747
"@eslint/js": "^9.29.0",
48+
"@rollup/plugin-replace": "^6.0.3",
4849
"@types/aws-lambda": "^8.10.150",
4950
"@types/morgan": "^1.9.10",
5051
"@types/sinonjs__fake-timers": "^15.0.1",

0 commit comments

Comments
 (0)