Skip to content

Commit 3f1b5a6

Browse files
committed
Add review comment suggestions
1 parent a70d335 commit 3f1b5a6

File tree

3 files changed

+319
-472
lines changed

3 files changed

+319
-472
lines changed

fixtures/vitest-pool-workers-examples/workflows/src/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,34 @@ export class TestLongWorkflow extends WorkflowEntrypoint<Env, Params> {
5454

5555
export default {
5656
async fetch(request: Request, env: Env) {
57-
const maybeId = new URL(request.url).searchParams.get("id");
57+
const url = new URL(request.url);
58+
const maybeId = url.searchParams.get("id");
5859
if (maybeId !== null) {
5960
const instance = await env.TEST_WORKFLOW.get(maybeId);
6061

6162
return Response.json(await instance.status());
6263
}
6364

64-
if (request.url.endsWith("/long-workflow")) {
65+
if (url.pathname === "/long-workflow") {
6566
const workflow = await env.TEST_LONG_WORKFLOW.create();
66-
return new Response(JSON.stringify({ id: workflow.id }));
67+
return Response.json({
68+
id: workflow.id,
69+
details: await workflow.status(),
70+
});
6771
}
6872

69-
if (request.url.endsWith("/long-workflow-batch")) {
73+
if (url.pathname === "/long-workflow-batch") {
7074
const workflows = await env.TEST_LONG_WORKFLOW.createBatch([
7175
{},
7276
{ id: "321" },
7377
{},
7478
]);
7579
const ids = workflows.map((workflow) => workflow.id);
76-
return new Response(JSON.stringify({ ids: ids }));
80+
return Response.json({ ids: ids });
7781
}
7882

7983
const workflow = await env.TEST_WORKFLOW.create();
8084

81-
return new Response(JSON.stringify({ id: workflow.id }));
85+
return Response.json({ id: workflow.id });
8286
},
8387
};
Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1-
import {
2-
env,
3-
introspectWorkflow,
4-
introspectWorkflowInstance,
5-
SELF,
6-
} from "cloudflare:test";
7-
import {
8-
afterEach,
9-
assert,
10-
beforeEach,
11-
describe,
12-
expect,
13-
it,
14-
vi,
15-
} from "vitest";
1+
import { env, introspectWorkflow, SELF } from "cloudflare:test";
2+
import { describe, expect, it, vi } from "vitest";
3+
4+
const STATUS_COMPLETE = "complete";
165

176
describe("Test Workflow", () => {
187
it("should be able to trigger a workflow", async () => {
@@ -30,78 +19,78 @@ describe("Test Workflow", () => {
3019
const res = await SELF.fetch(`https://mock-worker.local?id=${json.id}`);
3120

3221
const statusJson = await res.json<{ status: string }>();
33-
console.log(statusJson);
34-
return statusJson.status === "complete";
22+
expect(statusJson.status).toBe(STATUS_COMPLETE);
23+
return true;
3524
}, 1000);
3625
});
3726

3827
it("workflow should reach the end and be successful with introspector", async () => {
39-
const introspector = await introspectWorkflow(env.TEST_WORKFLOW);
28+
// CONFIG with `using` to ensure cleanup:
29+
await using introspector = await introspectWorkflow(env.TEST_WORKFLOW);
4030

4131
await SELF.fetch("https://mock-worker.local");
4232

4333
const instances = introspector.get();
44-
assert(instances.length === 1);
34+
expect(instances.length).toBe(1);
4535

36+
// ASSERTIONS:
4637
const instance = instances[0];
47-
await instance.waitForStatus("complete");
48-
await instance.cleanUp();
38+
await instance.waitForStatus(STATUS_COMPLETE);
39+
40+
// CLEANUP: assured by Symbol.asyncDispose
4941
});
5042
});
5143

5244
describe("Test long Workflow", () => {
5345
const STEP_NAME = "my step";
5446
const mockResult = "mocked result";
5547

56-
let introspector: Awaited<ReturnType<typeof introspectWorkflow>>;
57-
let instances: Awaited<ReturnType<typeof introspectWorkflowInstance>>[];
58-
59-
beforeEach(async () => {
60-
// CONFIG:
61-
introspector = await introspectWorkflow(env.TEST_LONG_WORKFLOW);
48+
it("workflow should be able to introspect and reach the end and be successful", async () => {
49+
// CONFIG with `using` to ensure cleanup:
50+
await using introspector = await introspectWorkflow(env.TEST_LONG_WORKFLOW);
6251
introspector.modifyAll(async (m) => {
6352
await m.disableSleeps();
6453
await m.mockStepResult({ name: STEP_NAME }, mockResult);
6554
});
66-
});
67-
68-
afterEach(async () => {
69-
// CLEANUP:
70-
// Instance introspectors should be clean to prevent persisted state across tests
71-
for (const instance of instances) {
72-
instance.cleanUp();
73-
}
74-
75-
// Workflow introspector should be cleaned at the end of/after each test
76-
introspector.cleanUp();
77-
});
7855

79-
it("workflow should be able to introspect and reach the end and be successful", async () => {
8056
await SELF.fetch("https://mock-worker.local/long-workflow");
8157

82-
instances = introspector.get();
83-
assert(instances.length === 1);
58+
const instances = introspector.get();
59+
expect(instances.length).toBe(1);
8460

8561
// ASSERTIONS:
8662
const instance = instances[0];
8763
expect(await instance.waitForStepResult({ name: STEP_NAME })).toEqual(
8864
mockResult
8965
);
90-
await instance.waitForStatus("complete");
66+
await instance.waitForStatus(STATUS_COMPLETE);
67+
68+
// CLEANUP: done by Symbol.asyncDispose
9169
});
9270

9371
it("workflow batch should be able to introspect and reach the end and be successful", async () => {
72+
let introspector = await introspectWorkflow(env.TEST_LONG_WORKFLOW);
73+
introspector.modifyAll(async (m) => {
74+
await m.disableSleeps();
75+
await m.mockStepResult({ name: STEP_NAME }, mockResult);
76+
});
77+
9478
await SELF.fetch("https://mock-worker.local/long-workflow-batch");
9579

96-
instances = introspector.get();
97-
assert(instances.length === 3);
80+
const instances = introspector.get();
81+
expect(instances.length).toBe(3);
9882

9983
// ASSERTIONS:
10084
for (const instance of instances) {
10185
expect(await instance.waitForStepResult({ name: STEP_NAME })).toEqual(
10286
mockResult
10387
);
104-
await instance.waitForStatus("complete");
88+
await instance.waitForStatus(STATUS_COMPLETE);
10589
}
90+
91+
// CLEANUP:
92+
// Workflow introspector should be cleaned at the end of/after each test, if no `using` keyword is used for the introspector
93+
// Cleans up all intercepted instances
94+
introspector.cleanUp();
10695
});
10796
});

0 commit comments

Comments
 (0)