|
| 1 | +import { |
| 2 | + introspectWorkflow, |
| 3 | + introspectWorkflowInstance, |
| 4 | +} from "cloudflare:test"; |
| 5 | +import { env, exports } from "cloudflare:workers"; |
| 6 | +import { it } from "vitest"; |
| 7 | + |
| 8 | +it("runs a Workflow declared in `exports` through `ctx.exports`", async ({ |
| 9 | + expect, |
| 10 | +}) => { |
| 11 | + const response = await exports.default.fetch( |
| 12 | + "https://example.com/?name=Workers" |
| 13 | + ); |
| 14 | + const { id } = await response.json<{ id: string }>(); |
| 15 | + |
| 16 | + await expect |
| 17 | + .poll(async () => { |
| 18 | + const response = await exports.default.fetch( |
| 19 | + `https://example.com/?id=${id}` |
| 20 | + ); |
| 21 | + return response.json(); |
| 22 | + }) |
| 23 | + .toMatchObject({ status: "complete", output: "Hello, Workers!" }); |
| 24 | +}); |
| 25 | + |
| 26 | +it("introspects instances created through `ctx.exports` with a binding", async ({ |
| 27 | + expect, |
| 28 | +}) => { |
| 29 | + await using introspector = await introspectWorkflow(env.GREETING_WORKFLOW); |
| 30 | + await introspector.modifyAll(async (m) => { |
| 31 | + await m.mockStepResult({ name: "greet" }, "Hello, mock!"); |
| 32 | + }); |
| 33 | + |
| 34 | + await exports.default.fetch("https://example.com/"); |
| 35 | + |
| 36 | + const instances = await introspector.get(); |
| 37 | + expect(instances).toHaveLength(1); |
| 38 | + await instances[0].waitForStatus("complete"); |
| 39 | + expect(await instances[0].getOutput()).toBe("Hello, mock!"); |
| 40 | +}); |
| 41 | + |
| 42 | +it("rejects introspecting a Workflow on `ctx.exports`", async ({ expect }) => { |
| 43 | + await expect(introspectWorkflow(exports.GreetingWorkflow)).rejects.toThrow( |
| 44 | + "`introspectWorkflow()` needs a Workflow binding from `env`" |
| 45 | + ); |
| 46 | + await expect( |
| 47 | + introspectWorkflowInstance(exports.GreetingWorkflow, "an-id") |
| 48 | + ).rejects.toThrow( |
| 49 | + "`introspectWorkflowInstance()` needs a Workflow binding from `env`" |
| 50 | + ); |
| 51 | +}); |
0 commit comments