Skip to content

Fix Vitest compatibility with the Cloudflare adapter#17243

Open
astrobot-houston wants to merge 2 commits into
mainfrom
triagebot/fix-15878
Open

Fix Vitest compatibility with the Cloudflare adapter#17243
astrobot-houston wants to merge 2 commits into
mainfrom
triagebot/fix-15878

Conversation

@astrobot-houston

Copy link
Copy Markdown
Contributor

Changes

  • Running Vitest with the @astrojs/cloudflare adapter now works. Previously, Vitest 4's module runner was fundamentally incompatible with @cloudflare/vite-plugin's workerd-based SSR environment due to three layered conflicts: (1) Vitest injects Node.js builtins into resolve.external, which the Cloudflare plugin's configResolved validation rejects; (2) Vitest disables the dep optimizer, causing CJS packages to be served as raw CJS to workerd's runner (which has no exports global); (3) Vitest's mocker plugin transforms dynamic import() calls, producing wrapDynamicImport errors when the mocker runtime isn't initialized in the SSR context.
  • When process.env.VITEST is set, @cloudflare/vite-plugin is skipped entirely, letting the SSR environment fall back to Vite's default Node.js-based DevEnvironment. Workerd-specific optimizeDeps config is also skipped. This follows the same pattern as isTypeGenPhase, which already bypasses workerd setup during type generation.
  • The astro:server plugin's configureServer hook now returns early under Vitest. The Astro dev server isn't needed for test runs, and its eager module imports through the runner triggered the mocker wrapDynamicImport error.

Closes #15878

Testing

  • No new tests added. The fix is a conditional skip guarded by process.env.VITEST; existing Cloudflare integration tests (130+ across ssr-deps, astro-dev-platform, astro-env, routing-priority, server-entry, cf-helpers) cover the unaffected non-Vitest path.

Docs

  • No docs update needed. This is a bug fix — users who previously hit this error no longer need a workaround and don't need to change their config.

Note: A changeset is missing from this branch. Both @astrojs/cloudflare and astro are modified and will need patch bumps before merging.

When running under Vitest (detected via process.env.VITEST), the
@cloudflare/vite-plugin and its workerd-based SSR environment are
fundamentally incompatible with Vitest's module runner due to multiple
layered conflicts:

1. Vitest injects Node.js builtins into resolve.external which the
   Cloudflare plugin validates and rejects
2. Vitest disables the dep optimizer, causing CJS packages to be served
   as raw CJS to workerd's module runner (which lacks the exports global)
3. Vitest's mocker plugin transforms dynamic import() calls in Astro's
   server code, causing wrapDynamicImport errors when the mocker runtime
   is not initialized in the SSR module runner context

The fix:
- Skips @cloudflare/vite-plugin entirely under Vitest, letting the SSR
  environment fall back to Vite's default Node.js-based DevEnvironment
- Skips workerd-specific optimizeDeps configuration under Vitest
- Skips Astro's dev server configureServer hook under Vitest (the dev
  server is not needed for running tests)

This follows the same pattern as isTypeGenPhase which already skips the
workerd setup during type generation.

Fixes #15878
…\n\nWhen running under Vitest (detected via process.env.VITEST), the\n@cloudflare/vite-plugin and its workerd-based SSR environment are\nfundamentally incompatible with Vitest's module runner due to multiple\nlayered conflicts:\n\n1. Vitest injects Node.js builtins into resolve.external which the\n Cloudflare plugin validates and rejects\n2. Vitest disables the dep optimizer, causing CJS packages to be served\n as raw CJS to workerd's module runner (which lacks the exports global)\n3. Vitest's mocker plugin transforms dynamic import() calls in Astro's\n server code, causing wrapDynamicImport errors when the mocker runtime\n is not initialized in the SSR module runner context\n\nThe fix:\n- Skips @cloudflare/vite-plugin entirely under Vitest, letting the SSR\n environment fall back to Vite's default Node.js-based DevEnvironment\n- Skips workerd-specific optimizeDeps configuration under Vitest\n- Skips Astro's dev server configureServer hook under Vitest (the dev\n server is not needed for running tests)\n\nThis follows the same pattern as isTypeGenPhase which already skips the\nworkerd setup during type generation.\n\nFixes #15878
@changeset-bot

changeset-bot Bot commented Jun 30, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: ae882b5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@github-actions github-actions Bot added pkg: integration Related to any renderer integration (scope) pkg: astro Related to the core `astro` package (scope) labels Jun 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Dependency Count

This PR adds 47 new dependencies (2102 → 2149), which exceeds the threshold of 10.

📊 Dependency Size Changes

Warning

This PR adds 188.5 MB of new dependencies, which exceeds the threshold of 100 kB.

📦 Package 📏 Size
@cloudflare/workerd-linux-64@1.20260630.1 127.3 MB
miniflare@4.20260630.0 19.8 MB
wrangler@4.106.0 18.6 MB
@esbuild/linux-x64@0.28.1 11.4 MB
workerd@1.20260630.1 4.8 MB
@vitest/browser@4.1.9 1.8 MB
@edge-runtime/primitives@6.0.0 1.7 MB
undici@7.28.0 1.6 MB
pngjs@7.0.0 650.1 kB
@vitest/mocker@4.1.9 187.4 kB
@vitest/utils@4.1.9 168.2 kB
ws@8.21.0 151.1 kB
esbuild@0.28.1 147 kB
@vitest/pretty-format@4.1.9 49.8 kB
@vitest/browser-playwright@4.1.9 48.6 kB
@edge-runtime/vm@5.0.0 48 kB
@vitest/spy@4.1.9 37.4 kB
@blazediff/core@1.9.1 21.6 kB

Total size change: 188.5 MB

@codspeed-hq

codspeed-hq Bot commented Jun 30, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing triagebot/fix-15878 (ae882b5) with main (d6ce7cd)

Open in CodSpeed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix verified Reporter confirmed the triage bot fix works pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope)

1 participant