Upgrade to Vitest 4.1, Vite 8, and @cloudflare/vitest-pool-workers 0.13#1138
Merged
Conversation
Add a vite decorator-transform plugin and wire it into many example/experimental vite.configs, migrate tests for Vitest (new env.d.ts files and numerous test updates), and add a changeset describing MCP schema conversion (replace dynamic import("ai") with z.fromJSONSchema and remove ensureJsonSchema). Also bump multiple example/experimental dependencies (kumo, tailwindcss, nanoid, viem, jose, postal-mime, cronstrue, etc.), update package.json/package-lock, add new scripts and patch files, and remove an old vitest-browser-react patch.
🦋 Changeset detectedLatest commit: e3803ff The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 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 |
agents
@cloudflare/ai-chat
@cloudflare/codemode
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
Bump peer dependency range to require Zod ^4.0.0 across packages and update the changeset to reflect Zod v4 and MCP tool schema conversion (replace dynamic import with z.fromJSONSchema(), remove ensureJsonSchema()). Add Vitest test setup that warms up the Cloudflare worker module graph (beforeAll exports.default.fetch) and retains a short afterAll delay to avoid noisy Durable Object close-handler logs. Add a new setup file and enable setupFiles for the think package, and increase a flaky resumable-streaming test delay from 200ms to 1000ms to reduce CI timeouts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Upgrades the test and build infrastructure to Vitest 4.1, Vite 8, and
@cloudflare/vitest-pool-workers0.13. This is a large migration that touches ~215 files across all packages, examples, and experimental projects.Key dependency changes
vitest3.2.4 → 4.1.0vite7.x → 8.0.1@cloudflare/vitest-pool-workers0.12.21 → 0.13.2@cloudflare/workers-types→ 4.20260317.1@vitest/browser-playwrightadded (new requirement for vitest 4)@rolldown/plugin-babel+@babel/plugin-proposal-decoratorsadded (oxc workaround)vite-plugin-devtools-jsonremoved (inlined intoscripts/)^3.25.0 || ^4.0.0→^4.0.0(required forz.fromJSONSchema())Migration guide
1. Vitest config:
defineWorkersConfig→cloudflareTestpluginThe old
defineWorkersConfigfrom@cloudflare/vitest-pool-workers/configis removed. The new API uses a Vite plugin:Important: All path-based options (
configPath,setupFiles,include,exclude) must use absolute paths viapath.join(import.meta.dirname, ...)— the new plugin resolves relative to the project root, not the config file.The
isolatedStorageandsingleWorkerpool options no longer exist.2. Test imports:
cloudflare:test→cloudflare:workersenvandSELFmoved out ofcloudflare:test:Test utilities (
createExecutionContext,runInDurableObject,introspectWorkflowInstance, etc.) remain incloudflare:test.3. Type declarations:
ProvidedEnv→Cloudflare.EnvThe old
ProvidedEnvinterface oncloudflare:testis replaced by augmenting theCloudflarenamespace from@cloudflare/workers-types:Key details:
declare namespace Cloudflarein a module file (any file withimport) must be wrapped indeclare global { ... }. We centralized all declarations intoenv.d.tsfiles to avoid this pitfall.type _WorkerEnv = import("./worker").Env) thenextends _WorkerEnv— usingextends import("./worker").Envdirectly doesn't propagate properties correctly.GlobalProps.mainModuletypesexports.defaultsoexports.default.fetch()is properly typed.typesarray changed:@cloudflare/vitest-pool-workers→@cloudflare/vitest-pool-workers/types(thecloudflare:testtypes moved to a subpath export).4. Agent class generics
With
Cloudflare.Envaugmented,Record<string, unknown>no longer satisfiesextends Cloudflare.Env. Test agent classes need updating:5. React browser test config
Vitest 4 changed the browser provider API:
render()fromvitest-browser-reactis now async — allrender()calls needawait.6.
vi.fn()mock constructorsVitest 4 no longer allows arrow functions in
vi.fn()to be used as constructors:7. DO stub RPC calls must be awaited
The new pool workers runs DO stubs via async RPC (no more
singleWorkermode). Calls likeagentStub.sqland other DO stub methods that were previously synchronous now needawait:8. Cold start warmup in test setup
The first
exports.default.fetch()or DO stub call triggers full Vite module graph resolution. In CI this can take >10s, causing the first test in each file to timeout. Add a warmup request in the setup file:Issues encountered and fixes
Oxc doesn't support TC39 decorators
Vite 8 replaced esbuild with oxc for TypeScript transforms. Oxc doesn't support TC39 decorators yet (oxc#9170), causing
SyntaxError: Invalid or unexpected tokenwhen workerd evaluates code with@callable().Fix: Added
@rolldown/plugin-babelwith@babel/plugin-proposal-decoratorsas a Vite plugin. Created a sharedscripts/vite-plugin-decorator-transform.tsused by all vitest configs and vite configs that process decorator syntax. Added to 18 example/experimental/site vite configs.Dynamic
import("ai")hangs in DO contextThe
await import("ai")inmcp/client.tscausedexports.default.fetch()to hang indefinitely when routing to a Durable Object in a monorepo workspace. The Vite module runner tried to lazy-load@ai-sdk/gateway(a transitive dep ofai) via RPC from inside the DO context, crossing workerd's DO I/O isolation boundary.Root cause: In the old vitest,
SELFdispatched through workerd's service binding mechanism (pre-bundled modules). The newexports.default.fetch()runs through Vite's module runner, which lazy-loads uncached modules on demand. In a workspace, the module graph is large enough that some transitive deps stay uncached until a DO is instantiated.Fix: Replaced
await import("ai")withz.fromJSONSchema()from zod (already a direct dependency). This eliminates theairuntime dependency entirely from the agents core —aiis now a type-only import. TheensureJsonSchema()method was removed as it's no longer needed. Zod peer dependency narrowed to^4.0.0sincez.fromJSONSchema()is a Zod 4-only API.Kumo theme override
The updated
@cloudflare/kumopackage uses Tailwind v4's@themeblocks for CSS custom properties.@themedeclarations are unlayered CSS that beats@layer baseregardless of source order. Theworkers.csstheme from@cloudflare/agents-uiwas in@layer base, so Kumo's blue brand color overrode the Workers orange.Fix: Moved
workers.cssdeclarations out of@layer baseto unlayered CSS. The[data-theme="workers"]selector has higher specificity than Kumo's:root, so it wins.vitest-browser-reactact() environmentUpdated the
vitest-browser-reactpatch for v2.1.0 — prevents the library from resettingIS_REACT_ACT_ENVIRONMENTtofalseafteract()calls complete (needed for ai-chat react tests). For agents react tests (integration tests with real WebSocket connections), wrappedrenderto reset the flag after mounting since async WebSocket updates legitimately happen outsideact().New shared utilities
scripts/vite-plugin-decorator-transform.tsscripts/vite-plugin-devtools-json.tsscripts/typecheck.tstsx scripts/typecheck.ts agents)packages/*/src/tests/env.d.tspackages/*/src/tests/setup.tspatches/vitest-browser-react+2.1.0.patchTest plan
packages/agentsworkers — 45/45 files, 745 tests pass, 8 skippedpackages/agentsreact — 14 tests pass (1 skipped)packages/agentsx402 — 33 tests passpackages/agentscli — 12 tests passpackages/agentse2e — 1 test passpackages/ai-chatworkers — 31/31 files, 297 tests passpackages/ai-chatreact — 28 tests passpackages/think— 7/7 files, 126 tests passpackages/voiceworkers — 5/5 files, 113 tests passpackages/voicereact — 152 tests passpackages/shell— 3/3 files, 189 tests passpackages/codemode— 8/8 files, 192 tests passpackages/worker-bundler— 3/3 files, 121 tests pass