Skip to content

Add vitest plugin for testing full workflows without setting up a server#1237

Merged
VaguelySerious merged 53 commits intomainfrom
peter/vitest-working
Mar 6, 2026
Merged

Add vitest plugin for testing full workflows without setting up a server#1237
VaguelySerious merged 53 commits intomainfrom
peter/vitest-working

Conversation

@VaguelySerious
Copy link
Member

@VaguelySerious VaguelySerious commented Mar 2, 2026

Superseeds #822. This allows users to add a vitest plugin enabling running of a workflow without a server instance, so you can e2e test a full workflow with given inputs.

Also introduces test helpers for "expect and skip next wait" and "expect and resolve next hook with ".

New docs link

Note this doesn't allow mocking due to how SWC bundles the step/run separately. There's a callout for that and it's already not supported in the existing docs/setup. I added a separate markdown file to the workbench with what Claude thinks is the way forward for that, and my personal take is that it's hard and we should not worry about it right now

cursoragent and others added 26 commits January 21, 2026 15:56
- Add new packages/vitest package with Vitest plugin that reuses @workflow/rollup
- Add workbench/vitest app with simple workflow code and unit tests
- Add GitHub CI workflow step for vitest plugin tests
- Add vitest workbench to changeset ignore list
- Step functions (add, multiply) now tested with actual execution and assertions
- Workflow function tested for workflowId property and expected throw behavior
- Removed greetWorkflow, simplified to single calculateWorkflow example
- Add test server (src/server.ts) that loads compiled workflow bundles
- Update tests to use start() to invoke workflows and verify results
- Tests verify calculateWorkflow(2, 7) returns correct sum/product/combined
- Build step uses 'wf build' to compile workflow bundles
- Add .gitignore for generated files
…ckage

- Delete packages/vitest (not needed as separate package)
- Update vitest.config.ts to use workflowTransformPlugin from @workflow/rollup
- Remove changeset entry for vitest package
- Remove duplicate test case
- Use globalSetup to start Nitro dev server before tests
- Use workflow/vite plugin for transformations
- Tests directly use start() and await run.returnValue
- Remove custom server.ts and build step
- Much simpler setup matching the sdk-with-workflows example
The vitest workbench spawns a Nitro server which doesn't work in the
regular unit test CI context. It has its own dedicated CI job.
- Add testing to docs navigation
- Create testing/index.mdx with setup instructions
- Link to workbench/vitest example files
Signed-off-by: Cursor Agent <cursoragent@cursor.com>
Signed-off-by: Cursor Agent <cursoragent@cursor.com>
Create new 'testing/' section and update Foundations index page.

Slack-Thread: https://vercel.slack.com/archives/C09125LC4AX/p1772375177894629?thread_ts=1772375177.894629&cid=C09125LC4AX
Co-authored-by: Pranay Prakash <1797812+pranaygp@users.noreply.github.com>
Update documentation to emphasize unit testing for steps only.

Slack-Thread: https://vercel.slack.com/archives/C09125LC4AX/p1772375177894629?thread_ts=1772375177.894629&cid=C09125LC4AX
Co-authored-by: Pranay Prakash <1797812+pranaygp@users.noreply.github.com>
Move testing to top-level, fix content and update meta files.

Slack-Thread: https://vercel.slack.com/archives/C09125LC4AX/p1772375177894629?thread_ts=1772375177.894629&cid=C09125LC4AX
Co-authored-by: Pranay Prakash <1797812+pranaygp@users.noreply.github.com>
Add 'globalSetup' script and vitest config, and update docs for test debugging.

Slack-Thread: https://vercel.slack.com/archives/C09125LC4AX/p1772375177894629?thread_ts=1772375177.894629&cid=C09125LC4AX
Co-authored-by: Pranay Prakash <1797812+pranaygp@users.noreply.github.com>
… sleeps

Exposes the existing `wakeUpRun` logic as an instance method on the `Run`
class, allowing users to skip sleep() calls in tests or custom UIs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Fix JSDoc @returns to describe StopSleepResult object instead of number
- Clean up global world state in afterEach to prevent test leakage
- Add delay before wakeUp() in docs example to allow workflow replay

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The file was removed on main but still exists on the base branch.
Restoring it so this PR's diff only contains relevant changes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The docs test type-checks each code block independently, so the
standalone wakeUp snippet needs its own imports and run variable.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Use the // @setup comment pattern to provide type context without
rendering the setup line, keeping the snippet lean.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Peter Wielander <mittgfu@gmail.com>
Signed-off-by: Peter Wielander <mittgfu@gmail.com>
@VaguelySerious VaguelySerious requested a review from a team as a code owner March 2, 2026 19:25
@changeset-bot
Copy link

changeset-bot bot commented Mar 2, 2026

🦋 Changeset detected

Latest commit: ff832ee

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
@workflow/world-local Patch
workflow Patch
@workflow/vitest Patch
@workflow/cli Patch
@workflow/core Patch
@workflow/world-postgres Patch
@workflow/world-testing Patch
@workflow/builders Patch
@workflow/next Patch
@workflow/nitro Patch
@workflow/web-shared Patch
@workflow/astro Patch
@workflow/nest Patch
@workflow/rollup Patch
@workflow/sveltekit Patch
@workflow/vite Patch
@workflow/nuxt Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Contributor

vercel bot commented Mar 2, 2026

@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2026

🧪 E2E Test Results

Some tests failed

Summary

Passed Failed Skipped Total
✅ ▲ Vercel Production 549 0 67 616
✅ 💻 Local Development 588 0 84 672
✅ 📦 Local Production 588 0 84 672
❌ 🐘 Local Postgres 587 1 84 672
✅ 🪟 Windows 53 0 3 56
❌ 🌍 Community Worlds 116 52 15 183
✅ 📋 Other 141 0 27 168
Total 2622 53 364 3039

❌ Failed Tests

🐘 Local Postgres (1 failed)

fastify-stable (1 failed):

  • webhookWorkflow
🌍 Community Worlds (52 failed)

mongodb (2 failed):

  • hookWorkflow is not resumable via public webhook endpoint
  • webhookWorkflow

redis (1 failed):

  • hookWorkflow is not resumable via public webhook endpoint

turso (49 failed):

  • addTenWorkflow
  • addTenWorkflow
  • wellKnownAgentWorkflow (.well-known/agent)
  • should work with react rendering in step
  • promiseAllWorkflow
  • promiseRaceWorkflow
  • promiseAnyWorkflow
  • importedStepOnlyWorkflow
  • hookWorkflow
  • hookWorkflow is not resumable via public webhook endpoint
  • webhookWorkflow
  • sleepingWorkflow
  • parallelSleepWorkflow
  • nullByteWorkflow
  • workflowAndStepMetadataWorkflow
  • fetchWorkflow
  • promiseRaceStressTestWorkflow
  • error handling error propagation workflow errors nested function calls preserve message and stack trace
  • error handling error propagation workflow errors cross-file imports preserve message and stack trace
  • error handling error propagation step errors basic step error preserves message and stack trace
  • error handling error propagation step errors cross-file step error preserves message and function names in stack
  • error handling retry behavior regular Error retries until success
  • error handling retry behavior FatalError fails immediately without retries
  • error handling retry behavior RetryableError respects custom retryAfter delay
  • error handling retry behavior maxRetries=0 disables retries
  • error handling retry behavior workflow completes despite transient 5xx on step_completed
  • error handling catchability FatalError can be caught and detected with FatalError.is()
  • hookCleanupTestWorkflow - hook token reuse after workflow completion
  • concurrent hook token conflict - two workflows cannot use the same hook token simultaneously
  • hookDisposeTestWorkflow - hook token reuse after explicit disposal while workflow still running
  • stepFunctionPassingWorkflow - step function references can be passed as arguments (without closure vars)
  • stepFunctionWithClosureWorkflow - step function with closure variables passed as argument
  • closureVariableWorkflow - nested step functions with closure variables
  • spawnWorkflowFromStepWorkflow - spawning a child workflow using start() inside a step
  • health check (queue-based) - workflow and step endpoints respond to health check messages
  • pathsAliasWorkflow - TypeScript path aliases resolve correctly
  • Calculator.calculate - static workflow method using static step methods from another class
  • AllInOneService.processNumber - static workflow method using sibling static step methods
  • ChainableService.processWithThis - static step methods using this to reference the class
  • thisSerializationWorkflow - step function invoked with .call() and .apply()
  • customSerializationWorkflow - custom class serialization with WORKFLOW_SERIALIZE/WORKFLOW_DESERIALIZE
  • instanceMethodStepWorkflow - instance methods with "use step" directive
  • crossContextSerdeWorkflow - classes defined in step code are deserializable in workflow context
  • stepFunctionAsStartArgWorkflow - step function reference passed as start() argument
  • cancelRun - cancelling a running workflow
  • cancelRun via CLI - cancelling a running workflow
  • pages router addTenWorkflow via pages router
  • pages router promiseAllWorkflow via pages router
  • pages router sleepingWorkflow via pages router

Details by Category

✅ ▲ Vercel Production
App Passed Failed Skipped
✅ astro 49 0 7
✅ example 49 0 7
✅ express 49 0 7
✅ fastify 49 0 7
✅ hono 49 0 7
✅ nextjs-turbopack 54 0 2
✅ nextjs-webpack 54 0 2
✅ nitro 49 0 7
✅ nuxt 49 0 7
✅ sveltekit 49 0 7
✅ vite 49 0 7
✅ 💻 Local Development
App Passed Failed Skipped
✅ astro-stable 47 0 9
✅ express-stable 47 0 9
✅ fastify-stable 47 0 9
✅ hono-stable 47 0 9
✅ nextjs-turbopack-canary 53 0 3
✅ nextjs-turbopack-stable 53 0 3
✅ nextjs-webpack-canary 53 0 3
✅ nextjs-webpack-stable 53 0 3
✅ nitro-stable 47 0 9
✅ nuxt-stable 47 0 9
✅ sveltekit-stable 47 0 9
✅ vite-stable 47 0 9
✅ 📦 Local Production
App Passed Failed Skipped
✅ astro-stable 47 0 9
✅ express-stable 47 0 9
✅ fastify-stable 47 0 9
✅ hono-stable 47 0 9
✅ nextjs-turbopack-canary 53 0 3
✅ nextjs-turbopack-stable 53 0 3
✅ nextjs-webpack-canary 53 0 3
✅ nextjs-webpack-stable 53 0 3
✅ nitro-stable 47 0 9
✅ nuxt-stable 47 0 9
✅ sveltekit-stable 47 0 9
✅ vite-stable 47 0 9
❌ 🐘 Local Postgres
App Passed Failed Skipped
✅ astro-stable 47 0 9
✅ express-stable 47 0 9
❌ fastify-stable 46 1 9
✅ hono-stable 47 0 9
✅ nextjs-turbopack-canary 53 0 3
✅ nextjs-turbopack-stable 53 0 3
✅ nextjs-webpack-canary 53 0 3
✅ nextjs-webpack-stable 53 0 3
✅ nitro-stable 47 0 9
✅ nuxt-stable 47 0 9
✅ sveltekit-stable 47 0 9
✅ vite-stable 47 0 9
✅ 🪟 Windows
App Passed Failed Skipped
✅ nextjs-turbopack 53 0 3
❌ 🌍 Community Worlds
App Passed Failed Skipped
✅ mongodb-dev 3 0 2
❌ mongodb 51 2 3
✅ redis-dev 3 0 2
❌ redis 52 1 3
✅ turso-dev 3 0 2
❌ turso 4 49 3
✅ 📋 Other
App Passed Failed Skipped
✅ e2e-local-dev-nest-stable 47 0 9
✅ e2e-local-postgres-nest-stable 47 0 9
✅ e2e-local-prod-nest-stable 47 0 9

📋 View full workflow run


Some E2E test jobs failed:

  • Vercel Prod: success
  • Local Dev: success
  • Local Prod: success
  • Local Postgres: failure
  • Windows: success

Check the workflow run for details.


let server: ChildProcess | null = null;
const PORT = "4000";
Use [`start()`](/docs/api-reference/workflow-api/start) to trigger a workflow and `await run.returnValue` to get the result:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's cross link to run.returnValue docs in API reference? (so people/agents can figure out that it will block till ready, and throw if the workflow fails, etc.)

@@ -0,0 +1,3 @@
import { setupWorkflowTests } from './index.js';

await setupWorkflowTests();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setup-file.ts calls setupWorkflowTests() but never calls teardownWorkflowTests(). The manual setup example in the docs shows afterAll(() => teardownWorkflowTests()), but the automatic path skips this. This could leak resources or let state bleed between test files in a multi-file suite.

Consider adding teardown registration here:

import { setupWorkflowTests, teardownWorkflowTests } from "./index.js";
import { afterAll } from "vitest";

await setupWorkflowTests();

afterAll(async () => {
  await teardownWorkflowTests();
});

Copy link
Member

@TooTallNate TooTallNate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Great feature — running full workflow integration tests in-process without a server is a significant DX improvement. The core approach of registering direct handlers on the local world queue is elegant and minimal.

What looks good

  • Clean architecture: globalSetup builds bundles, setupFile wires up in-process handlers via registerHandler()
  • waitForSleep() and waitForHook() test helpers are well-designed with sensible polling + timeout defaults
  • Docs rewrite is excellent — leading with the simpler in-process approach and moving server-based to its own page
  • Honest callout about vi.mock() limitations, with thorough internal analysis in MOCKING.md
  • CI properly isolates the vitest workbench tests into their own job
  • Good test coverage in the workbench (simple, sleeping, hooks, webhooks)

Items to address

  1. Missing teardown in setup-file.ts — resources/state may leak between test files
  2. Prefix detection in queue.ts — falls through to __wkf_workflow_ for unknown queue names (works by accident)
  3. Stale data directory.workflow-vitest/data persists between runs, risking event collisions
  4. Missing peer dependenciesvite and vitest should be peer deps in @workflow/vitest
  5. Stale workbench README — references Nitro server approach and files that no longer exist

Minor / non-blocking

  • resolveBaseUrl moved inside the retry loop for the HTTP path (was previously hoisted)
  • Incidental formatting changes in .changeset/config.json
  • undici 7.16.0 -> 7.22.0 bump in lockfile (harmless but unrelated)

Overall this is close to ready. The teardown and data isolation issues are the most important to address before merge.

VaguelySerious and others added 4 commits March 4, 2026 12:16
Signed-off-by: Peter Wielander <mittgfu@gmail.com>
- Add afterAll teardown to setup-file.ts (TooTallNate)
- Add vite/vitest peerDependencies to @workflow/vitest (TooTallNate)
- Fix queue.ts prefix check to be explicit (TooTallNate)
- Update stale workbench README to match current setup (TooTallNate)
- Switch webhook test to use resumeWebhook (pranaygp)
- Fix meta.json page reference from "testing" to "index" (pranaygp)
- Update integration test description (pranaygp)
- Document test isolation: world data cleared on setup (pranaygp)
- Add run.status example and cross-link to run.returnValue (pranaygp)
- Document wakeUp without correlationIds behavior (pranaygp)
- Document waitForSleep parallel/sequential behavior (pranaygp)
- Use resumeWebhook in webhook docs example (pranaygp)
- Add @workflow/vitest API reference page (pranaygp)
- Clean up server-based.mdx: remove hallucinated frontmatter, add
  hooks/waits info, note about customization (pranaygp)
- Fix prose references to workflow/vitest → @workflow/vitest (vercel[bot])
- Test third-party mocking: confirmed vi.mock does not work for npm
  packages either, updated MOCKING.md and callout (VaguelySerious)
- Fix per-worker data directory isolation (VITEST_POOL_ID)
- Add changesets for @workflow/vitest and @workflow/world-local

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Peter Wielander <mittgfu@gmail.com>
The docs-typecheck project doesn't have @workflow/vitest installed,
so code samples using its exports fail type checking. Add
@skip-typecheck annotations to those blocks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
vercel bot and others added 4 commits March 4, 2026 21:15
…ith "Unexpected character `!` (U+0021) before name" error.

This commit fixes the issue reported at docs/content/docs/api-reference/vitest/index.mdx:88

**Bug:** The file `docs/content/docs/api-reference/vitest/index.mdx` uses HTML-style comments (`<!-- @skip-typecheck ... -->`) on lines 88 and 117. MDX does not support HTML comment syntax. When the MDX compiler encounters `<!--`, it sees `!` after `<` and expects a valid JSX element name (starting with a letter, `$`, or `_`), causing a parse error.

The build log from deployment `dpl_AcUbRm42SaELfM8UKd2nrVPDsxEE` confirms this:
```
docs:build: 84:2: index.mdx:84:2: Unexpected character `!` (U+0021) before name, expected a character that can start a name, such as a letter, `$`, or `_` (note: to create a comment in MDX, use `{/* text */}`)
```

This causes the entire docs build to fail (`docs#build` is listed as the failed task).

**Fix:** Replaced both HTML-style comments with the correct MDX/JSX comment syntax:
- `<!-- @skip-typecheck - @workflow/vitest not available in docs-typecheck -->` → `{/* @skip-typecheck - @workflow/vitest not available in docs-typecheck */}`

This is consistent with how all other MDX files in the `docs/content` directory handle `@skip-typecheck` comments (e.g., `docs/content/docs/how-it-works/understanding-directives.mdx` uses `{/* @skip-typecheck: ... */}` throughout).

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: VaguelySerious <mittgfu@gmail.com>
Signed-off-by: Peter Wielander <mittgfu@gmail.com>
Copy link
Member

@TooTallNate TooTallNate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary (follow-up)

All previous feedback from both my review and @pranaygp's review has been addressed. The updates are well done. Here's the status:

Previous Feedback — All Resolved

Item Status
Missing teardown in setup-file.ts Fixed — afterAll(() => teardownWorkflowTests()) added
Stale data directory / test isolation Fixed — world.clear() on setup + per-worker VITEST_POOL_ID dirs
Missing peer dependencies Fixed — vite >=6.0.0 and vitest >=3.0.0 added
Stale workbench README Fixed — fully rewritten
Queue prefix falls through for unknown names Fixed — explicit if/else if/else throw
Missing changesets (pranaygp) Fixed — new changesets for @workflow/vitest and @workflow/world-local
Separate @workflow/vitest package (pranaygp) Fixed — no longer bundled in workflow/vitest
meta.json pages (pranaygp) Fixed — ["index", "server-based"]
Hallucinated front matter (pranaygp) Fixed — removed
Server-based page framing (pranaygp) Fixed — noted as starting point
vi.mock() callout (pranaygp) Fixed — clear warning callout added
Test isolation docs (pranaygp) Fixed — documented
run.status + returnValue blocking (pranaygp) Fixed — example updated
Cross-link to run.returnValue docs (pranaygp) Fixed
wakeUp() without correlationIds comment (pranaygp) Fixed
waitForSleep multi-sleep behavior (pranaygp) Fixed — dedicated callout
Use resumeWebhook for webhooks (pranaygp) Fixed
API reference for @workflow/vitest (pranaygp) Fixed — full page at docs/api-reference/vitest/

CI Failures to Fix

Two CI jobs are failing on the latest commit:

1. Vitest Plugin Tests — webhook test fails

Error: `createWebhook()` does not accept a `token` option. Webhook tokens are always
randomly generated. Use `createHook()` with `resumeHook()` for deterministic token patterns.

The webhookWorkflow in workbench/vitest/workflows/webhook.ts passes a token option to createWebhook(), but it looks like createWebhook now rejects custom tokens. The test and workflow need to be updated to match the current createWebhook API — either use a randomly generated token (and discover it via waitForHook without a token filter, using the hook object's token to call resumeWebhook), or switch to createHook if deterministic tokens are needed. The docs webhook example (docs/content/docs/testing/index.mdx:258) also uses token in createWebhook and would need the same fix.

2. Docs Code Samples — type-check failures in api-reference/vitest/index.mdx

Three code samples fail type-checking:

  • Line 15: defineConfig not imported (missing import { defineConfig } from "vitest/config")
  • Line 32: buildWorkflowTests not imported (missing import from @workflow/vitest)
  • Line 52: beforeAll/afterAll not imported (missing import from vitest)

These are the shorter code snippets in the API reference page that don't include their imports. Either add the imports or use @skip-typecheck comments like the test helper examples already do.

Minor Nits (non-blocking)

  • Both vite and vitest peer deps are marked optional. Consider making vitest non-optional since the primary use case requires it.
  • buildTarget: 'next' in VitestBuilder (packages/vitest/src/index.ts:22) — a brief comment explaining why 'next' is the right target for vitest builds would help future readers.

VaguelySerious and others added 4 commits March 5, 2026 15:22
- Fix webhookWorkflow: createWebhook() does not accept token option,
  tokens are randomly generated. Use waitForHook() to discover token.
- Update docs webhook example to match (no custom token)
- Add @skip-typecheck to remaining API ref code samples
- Make vitest peerDep non-optional (vite stays optional)
- Add buildTarget comment explaining why 'next' is used

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Peter Wielander <mittgfu@gmail.com>
Signed-off-by: Peter Wielander <mittgfu@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants