You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/workers/testing/vitest-integration/test-apis.mdx
+66-5Lines changed: 66 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,6 @@ description: Runtime helpers for writing tests, exported from the `cloudflare:te
8
8
9
9
---
10
10
11
-
12
11
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.
13
12
14
13
## `cloudflare:test` module definition
@@ -282,7 +281,10 @@ To test projects that use Workflows `isolatedStorage` **must** be set to `false`
@@ -299,7 +301,7 @@ To test projects that use Workflows `isolatedStorage` **must** be set to `false`
299
301
*`modify(fn: (m: WorkflowInstanceModifier) => Promise<void>)`: Applies modifications to the Workflow instance's behavior.
300
302
*`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.
301
303
*`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.
* 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`
@@ -348,5 +353,61 @@ To test projects that use Workflows `isolatedStorage` **must** be set to `false`
348
353
* `forceStepTimeout(step: { name: string; index?: number }, times?: number)`: Forces a `step.do()` to fail by timing out immediately.
349
354
* `mockEvent(event: { type: string; payload: unknown })`: Sends a mock event to the Workflow instance, causing a `step.waitForEvent()` to resolve with the provided payload.
350
355
* `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 () => {
// Given the forced timeouts, the workflow will end in an errored state
405
+
awaitinstance.waitForStatus("errored");
406
+
407
+
// 4. CLEANUP
408
+
awaitinstance.cleanUp();
409
+
});
410
+
```
351
411
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.
0 commit comments