Fix Vitest compatibility with the Cloudflare adapter#17243
Open
astrobot-houston wants to merge 2 commits into
Open
Fix Vitest compatibility with the Cloudflare adapter#17243astrobot-houston wants to merge 2 commits into
astrobot-houston wants to merge 2 commits into
Conversation
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
1 task
|
Contributor
|
| 📦 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
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.
Changes
@astrojs/cloudflareadapter 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 intoresolve.external, which the Cloudflare plugin'sconfigResolvedvalidation rejects; (2) Vitest disables the dep optimizer, causing CJS packages to be served as raw CJS to workerd's runner (which has noexportsglobal); (3) Vitest's mocker plugin transforms dynamicimport()calls, producingwrapDynamicImporterrors when the mocker runtime isn't initialized in the SSR context.process.env.VITESTis set,@cloudflare/vite-pluginis skipped entirely, letting the SSR environment fall back to Vite's default Node.js-basedDevEnvironment. Workerd-specificoptimizeDepsconfig is also skipped. This follows the same pattern asisTypeGenPhase, which already bypasses workerd setup during type generation.astro:serverplugin'sconfigureServerhook 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 mockerwrapDynamicImporterror.Closes #15878
Testing
process.env.VITEST; existing Cloudflare integration tests (130+ acrossssr-deps,astro-dev-platform,astro-env,routing-priority,server-entry,cf-helpers) cover the unaffected non-Vitest path.Docs