Skip to content

Commit bf920ca

Browse files
committed
Add suggestions
1 parent 5fd159d commit bf920ca

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

src/content/docs/workers/testing/vitest-integration/test-apis.mdx

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ description: Runtime helpers for writing tests, exported from the `cloudflare:te
88

99
---
1010

11-
1211
The Workers Vitest integration provides runtime helpers for writing tests in the `cloudflare:test` module. The `cloudflare:test` module is provided by the `@cloudflare/vitest-pool-workers` package, but can only be imported from test files that execute in the Workers runtime.
1312

1413
## `cloudflare:test` module definition
@@ -282,7 +281,10 @@ To test projects that use Workflows `isolatedStorage` **must** be set to `false`
282281
const instance = await introspectWorkflowInstance(env.MY_WORKFLOW, "123456");
283282
await instance.modify(async (m) => {
284283
await m.disableSleeps();
285-
await m.mockEvent({ type: "user-approval" }, { approval: true });
284+
await m.mockEvent({
285+
type: "user-approval",
286+
payload: { approved: true, approverId: "user-123" },
287+
});
286288
});
287289

288290
// 2. EXECUTION
@@ -299,7 +301,7 @@ To test projects that use Workflows `isolatedStorage` **must** be set to `false`
299301
* `modify(fn: (m: WorkflowInstanceModifier) => Promise<void>)`: Applies modifications to the Workflow instance's behavior.
300302
* `waitForStepResult(step: { name: string; index?: number })`: Waits for a specific step to complete and return a result. If multiple steps share the same name, use the optional `index` property (1-based, defaults to `1`) to target a specific occurrence.
301303
* `waitForStatus(status: InstanceStatus["status"])`: Waits for the Workflow instance to reach a specific [status](/workflows/build/workers-api/#instancestatus) (e.g., 'running', 'complete').
302-
* `cleanUp()`: Cleans up the Workflow instance's state. It is crucial to call this after each test to ensure isolation.
304+
* `cleanUp()`: Cleans up the Workflow instance's state, which is crucial for test isolation. If this function isn't called, the instance's state will persist across subsequent tests. For example, an instance that becomes completed in one test will already be completed at the start of the next.
303305

304306
* `introspectWorkflow(workflow: Workflow)`: Promise\<WorkflowIntrospector>
305307
* Creates an **introspector** for a Workflow where instance IDs are unknown beforehand. This allows for defining modifications that will apply to **all subsequently created instances**.
@@ -313,7 +315,10 @@ To test projects that use Workflows `isolatedStorage` **must** be set to `false`
313315
const introspector = await introspectWorkflow(env.MY_WORKFLOW);
314316
await introspector.modifyAll(async (m) => {
315317
await m.disableSleeps();
316-
await m.mockEvent({ type: "user-approval" }, { approval: true });
318+
await m.mockEvent({
319+
type: "user-approval",
320+
payload: { approved: true, approverId: "user-123" },
321+
});
317322
});
318323

319324
// 2. EXECUTION
@@ -348,5 +353,61 @@ To test projects that use Workflows `isolatedStorage` **must** be set to `false`
348353
* `forceStepTimeout(step: { name: string; index?: number }, times?: number)`: Forces a `step.do()` to fail by timing out immediately.
349354
* `mockEvent(event: { type: string; payload: unknown })`: Sends a mock event to the Workflow instance, causing a `step.waitForEvent()` to resolve with the provided payload.
350355
* `forceEventTimeout(step: { name: string; index?: number })`: Forces a `step.waitForEvent()` to time out instantly, causing the step to fail.
356+
357+
<br/>
358+
```ts
359+
import { env, introspectWorkflowInstance } from "cloudflare:test";
360+
361+
it("should apply all modifier functions", async () => {
362+
// 1. CONFIGURATION
363+
const instance = await introspectWorkflowInstance(env.COMPLEX_WORKFLOW, "123456");
364+
365+
// Modify instance behavior
366+
await instance.modify(async (m) => {
367+
// Disables all sleeps to make the test run instantly
368+
await m.disableSleeps();
369+
370+
// Mocks the successful result of a data-fetching step
371+
await m.mockStepResult(
372+
{ name: "get-order-details" },
373+
{ orderId: "abc-123", amount: 99.99 }
374+
);
375+
376+
// Mocks an incoming event to satisfy a `step.waitForEvent()`
377+
await m.mockEvent({
378+
type: "user-approval",
379+
payload: { approved: true, approverId: "user-123" },
380+
});
381+
382+
// Forces a step to fail once with a specific error to test retry logic
383+
await m.mockStepError(
384+
{ name: "process-payment" },
385+
new Error("Payment gateway timeout"),
386+
1 // Fail only the first time
387+
);
388+
389+
// Forces a `step.do()` to time out immediately
390+
await m.forceStepTimeout({ name: "notify-shipping-partner" });
391+
392+
// Forces a `step.waitForEvent()` to time out
393+
await m.forceEventTimeout({ name: "wait-for-fraud-check" });
394+
});
395+
396+
// 2. EXECUTION
397+
await env.COMPLEX_WORKFLOW.create({ id: "123456" });
398+
399+
// 3. ASSERTION
400+
expect(await instance.waitForStepResult({ name: "get-order-details" })).toEqual({
401+
orderId: "abc-123",
402+
amount: 99.99,
403+
});
404+
// Given the forced timeouts, the workflow will end in an errored state
405+
await instance.waitForStatus("errored");
406+
407+
// 4. CLEANUP
408+
await instance.cleanUp();
409+
});
410+
```
351411

352-
When targeting a step, use its `name`. If multiple steps share the same name, use the optional `index` property (1-based, defaults to `1`) to specify the occurrence.
412+
When targeting a step, use its `name`. If multiple steps share the same name, use the optional `index` property (1-based, defaults to `1`) to specify the occurrence.
413+

src/content/docs/workflows/build/test-workflows.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@ pcx_content_type: navigation
44
external_link: /workers/testing/vitest-integration/test-apis/#workflows
55
sidebar:
66
order: 11
7-
badge:
8-
text: Beta
97
---

0 commit comments

Comments
 (0)