Add vitest plugin for testing full workflows without setting up a server#1237
Add vitest plugin for testing full workflows without setting up a server#1237VaguelySerious merged 53 commits intomainfrom
Conversation
- 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>
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>
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>
🦋 Changeset detectedLatest commit: ff832ee The changes in this PR will be included in the next version bump. This PR includes changesets to release 17 packages
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 |
🧪 E2E Test Results❌ Some tests failed Summary
❌ Failed Tests🐘 Local Postgres (1 failed)fastify-stable (1 failed):
🌍 Community Worlds (52 failed)mongodb (2 failed):
redis (1 failed):
turso (49 failed):
Details by Category✅ ▲ Vercel Production
✅ 💻 Local Development
✅ 📦 Local Production
❌ 🐘 Local Postgres
✅ 🪟 Windows
❌ 🌍 Community Worlds
✅ 📋 Other
❌ Some E2E test jobs failed:
Check the workflow run for details. |
docs/content/docs/testing/index.mdx
Outdated
|
|
||
| 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: |
There was a problem hiding this comment.
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(); | |||
There was a problem hiding this comment.
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();
});
TooTallNate
left a comment
There was a problem hiding this comment.
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:
globalSetupbuilds bundles,setupFilewires up in-process handlers viaregisterHandler() waitForSleep()andwaitForHook()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 inMOCKING.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
- Missing teardown in
setup-file.ts— resources/state may leak between test files - Prefix detection in
queue.ts— falls through to__wkf_workflow_for unknown queue names (works by accident) - Stale data directory —
.workflow-vitest/datapersists between runs, risking event collisions - Missing peer dependencies —
viteandvitestshould be peer deps in@workflow/vitest - Stale workbench README — references Nitro server approach and files that no longer exist
Minor / non-blocking
resolveBaseUrlmoved inside the retry loop for the HTTP path (was previously hoisted)- Incidental formatting changes in
.changeset/config.json undici7.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.
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>
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>
…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>
TooTallNate
left a comment
There was a problem hiding this comment.
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:
defineConfignot imported (missingimport { defineConfig } from "vitest/config") - Line 32:
buildWorkflowTestsnot imported (missing import from@workflow/vitest) - Line 52:
beforeAll/afterAllnot imported (missing import fromvitest)
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
viteandvitestpeer deps are marked optional. Consider makingvitestnon-optional since the primary use case requires it. buildTarget: 'next'inVitestBuilder(packages/vitest/src/index.ts:22) — a brief comment explaining why'next'is the right target for vitest builds would help future readers.
- 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>
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