Skip to content

Commit 9a3d43c

Browse files
LuisDuarte1petebacondarwinpenalosa
authored
fix: make vitest-pool-workers not crash with scripts that have Workflows (#8208)
* fix: make vitest-pool-workers not crash with scripts that have Workflows Because `vitest-pool-workers` uses its runner worker as the entrypoint and not the user provided worker. The user provided worker is loaded AFAICT though eval (`vitest-pool-workers` enables eval) so we need, at startup time, to add the required entrypoints and make them dynamically call the user one. Not only that, if you try to create a workflow instance it would throw a `isolated storage` error - I suspect that is this caused by engine still being active at the end of the test (since workflows are async, not in the request path, and might take undefinite time to complete). In order to fix this, I've bypassed the isolated storage check for workflows internal DOs. I wonder if there might be collisions between test files if there are any concurrency regarding test files (aka `singleWorker` is false) - I think that this is fine to do, the point of this PR is not to make workflows really testable yet but to at least to make `vitest-pool-workers` **not** crash if a user includes a workflow in the worker definition. * chore: add proper readme * chore: remove isolatedStorage bypass and error out early if isolated storage is used * chore: add suggestions * chore: moar test * Apply suggestions from code review Co-authored-by: Pete Bacon Darwin <[email protected]> * Apply suggestions from code review Co-authored-by: Pete Bacon Darwin <[email protected]> * stream logs * no more hanging * Revert "stream logs" This reverts commit dfecf63. --------- Co-authored-by: Pete Bacon Darwin <[email protected]> Co-authored-by: Samuel Macleod <[email protected]>
1 parent a236115 commit 9a3d43c

File tree

14 files changed

+238
-2
lines changed

14 files changed

+238
-2
lines changed

.changeset/vast-stars-dig.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vitest-pool-workers": patch
3+
---
4+
5+
Make vitest-pool-workers not crash when a workflow is defined
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 🔁 workflows
2+
3+
Unit testing of workflows themselves is not possible yet - but you can still
4+
trigger them in unit tests.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface Env {
2+
TEST_WORKFLOW: Workflow;
3+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
WorkerEntrypoint,
3+
WorkflowEntrypoint,
4+
WorkflowEvent,
5+
WorkflowStep,
6+
} from "cloudflare:workers";
7+
8+
export class TestWorkflow extends WorkflowEntrypoint<Env> {
9+
async run(_event: Readonly<WorkflowEvent<unknown>>, step: WorkflowStep) {
10+
return "test-workflow";
11+
}
12+
}
13+
14+
export default class TestNamedEntrypoint extends WorkerEntrypoint<Env> {
15+
async fetch(request: Request) {
16+
const maybeId = new URL(request.url).searchParams.get("id");
17+
if (maybeId !== null) {
18+
const instance = await this.env.TEST_WORKFLOW.get(maybeId);
19+
20+
return Response.json(await instance.status());
21+
}
22+
23+
const workflow = await this.env.TEST_WORKFLOW.create();
24+
25+
return new Response(JSON.stringify({ id: workflow.id }));
26+
}
27+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.workerd.json",
3+
"include": ["./**/*.ts"]
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare module "cloudflare:test" {
2+
// Controls the type of `import("cloudflare:test").env`
3+
interface ProvidedEnv extends Env {
4+
TEST_WORKFLOW: Workflow;
5+
}
6+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { SELF } from "cloudflare:test";
2+
import { expect, it, vi } from "vitest";
3+
4+
it("should be able to trigger a workflow", async () => {
5+
const res = await SELF.fetch("https://mock-worker.local");
6+
7+
expect(res.status).toBe(200);
8+
});
9+
10+
it("workflow should reach the end and be successful", async () => {
11+
const res = await SELF.fetch("https://mock-worker.local");
12+
13+
const json = await res.json<{ id: string }>();
14+
15+
await vi.waitUntil(async () => {
16+
const res = await SELF.fetch(`https://mock-worker.local?id=${json.id}`);
17+
18+
const statusJson = await res.json<{ status: string }>();
19+
console.log(statusJson);
20+
return statusJson.status === "complete";
21+
}, 1000);
22+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.workerd-test.json",
3+
"include": ["./**/*.ts", "../src/env.d.ts"]
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../tsconfig.node.json",
3+
"include": ["./*.ts"]
4+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config";
2+
3+
export default defineWorkersProject({
4+
esbuild: {
5+
// Required for `using` support
6+
target: "ES2022",
7+
},
8+
test: {
9+
poolOptions: {
10+
workers: {
11+
singleWorker: true,
12+
// FIXME(lduarte): currently for the workflow binding to work, isolateStorage must be disabled.
13+
isolatedStorage: false,
14+
wrangler: {
15+
configPath: "./wrangler.toml",
16+
},
17+
},
18+
},
19+
},
20+
});

0 commit comments

Comments
 (0)