Repository navigation
Vitest working with Nitro v2, but not with Nitro v3.0.1-alpha.0 #3659
Copy link
Copy link
Closed
Labels
Description
Environment
npx --yes envinfo --system --binaries
System:
OS: Linux 6.17 Arch Linux
Container: Yes
Shell: 5.9 - /usr/bin/zsh
Binaries:
Node: 24.10.0
npm: 11.6.2
pnpm: 10.18.3Reproduction
https://github.com/theoludwig/vite-nitro-issues
Unfortunately this is not a "as minimal as possible reproduction", but I thought it was still worth to open the issue as an early tester of Nitro v3 alpha version, and that it was working with v2. The provided reproduction is a TanStack Start application following their documentation about Nitro usage: https://tanstack.com/start/latest/docs/framework/react/guide/hosting#using-nitro-v2
I haven't had time to investigate the issue further yet.
Describe the bug
node --run test which is using vitest run doesn't work with Nitro v3, but works with Nitro v2.
I did 2 commits in the reproduction example:
- chore: nitro v2 which use
@tanstack/nitro-v2-vite-pluginand has a green CI, no problems. - chore: nitro v3, with as you can see in the commit, the only change, is that it's using
"nitro": "3.0.1-alpha.0", and the CI doesn't work (same behavior as locally), and fails withvitest run.
Additional context
No response
Logs
info Using tsconfig paths for react-docgen
⎯⎯⎯⎯⎯⎯⎯ Startup Error ⎯⎯⎯⎯⎯⎯⎯⎯
TypeError: Cannot read properties of undefined (reading 'onMessage')
at Object.on (file:///home/runner/work/vite-nitro-issues/vite-nitro-issues/node_modules/.pnpm/nitro@3.0.1-alpha.0_vite@7.1.10_@types+node@24.7.2_jiti@2.6.1_lightningcss@1.30.1_tsx@4.20.6_/node_modules/nitro/dist/_chunks/plugin.mjs:348:13)
at Object.setInvokeHandler (file:///home/runner/work/vite-nitro-issues/vite-nitro-issues/node_modules/.pnpm/vite@7.1.10_@types+node@24.7.2_jiti@2.6.1_lightningcss@1.30.1_tsx@4.20.6/node_modules/vite/dist/node/chunks/dep-B0GuR2De.js:28475:16)
at new DevEnvironment (file:///home/runner/work/vite-nitro-issues/vite-nitro-issues/node_modules/.pnpm/vite@7.1.10_@types+node@24.7.2_jiti@2.6.1_lightningcss@1.30.1_tsx@4.20.6/node_modules/vite/dist/node/chunks/dep-B0GuR2De.js:35006:12)
at new FetchableDevEnvironment (file:///home/runner/work/vite-nitro-issues/vite-nitro-issues/node_modules/.pnpm/nitro@3.0.1-alpha.0_vite@7.1.10_@types+node@24.7.2_jiti@2.6.1_lightningcss@1.30.1_tsx@4.20.6_/node_modules/nitro/dist/_chunks/plugin.mjs:318:5)
at createFetchableDevEnvironment (file:///home/runner/work/vite-nitro-issues/vite-nitro-issues/node_modules/.pnpm/nitro@3.0.1-alpha.0_vite@7.1.10_@types+node@24.7.2_jiti@2.6.1_lightningcss@1.30.1_tsx@4.20.6_/node_modules/nitro/dist/_chunks/plugin.mjs:313:10)
at Object.createEnvironment (file:///home/runner/work/vite-nitro-issues/vite-nitro-issues/node_modules/.pnpm/nitro@3.0.1-alpha.0_vite@7.1.10_@types+node@24.7.2_jiti@2.6.1_lightningcss@1.30.1_tsx@4.20.6_/node_modules/nitro/dist/_chunks/plugin.mjs:525:50)
at _createServer (file:///home/runner/work/vite-nitro-issues/vite-nitro-issues/node_modules/.pnpm/vite@7.1.10_@types+node@24.7.2_jiti@2.6.1_lightningcss@1.30.1_tsx@4.20.6/node_modules/vite/dist/node/chunks/dep-B0GuR2De.js:27964:132)
at async createViteServer (file:///home/runner/work/vite-nitro-issues/vite-nitro-issues/node_modules/.pnpm/vitest@3.2.4_@types+node@24.7.2_@vitest+browser@3.2.4_jiti@2.6.1_lightningcss@1.30.1_tsx@4.20.6/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:6911:17)
at async createVitest (file:///home/runner/work/vite-nitro-issues/vite-nitro-issues/node_modules/.pnpm/vitest@3.2.4_@types+node@24.7.2_@vitest+browser@3.2.4_jiti@2.6.1_lightningcss@1.30.1_tsx@4.20.6/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:10202:17)
at async prepareVitest (file:///home/runner/work/vite-nitro-issues/vite-nitro-issues/node_modules/.pnpm/vitest@3.2.4_@types+node@24.7.2_@vitest+browser@3.2.4_jiti@2.6.1_lightningcss@1.30.1_tsx@4.20.6/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:10541:14)Reacted by Robert Buchberger and Patt O'Brien
Activity
hi-ogawa commentedon Oct 17, 2025
For unit testing, server runtime is irrelevant, so
nitroshould be disabled. One quick way is to skip the plugin based onprocess.env.VITESTsuch asplugins: [!process.env.VITEST && nitro()]. Tanstack start's Nitro v2 plugin was essentially build only, so it didn't manifest any issues.chore: do not use nitro in vitest
theoludwig commentedon Oct 18, 2025
Ah, okay, it makes sense, thanks for the reply.
I tried it, and indeed it seems to work: theoludwig/vite-nitro-issues@8255b85
pi0 commentedon Oct 19, 2025
reopening issue to try to make nitro plugin behavior smarter when running within tests.
kricsleo commentedon Oct 21, 2025
Means Nitro could skip internally when detected running with tests? If that's it, I can help. 🙌
theoludwig commentedon Nov 10, 2025
Is the above could be a solution? @pi0
For now users, must have in their
vite.config.tsthe following:pi0 commentedon Nov 10, 2025
@theoludwig yes it is a good workaround.
i am investigating first-class vitest support, nitro already can setup a good vitest runner enviornment small changes needed.
hi-ogawa commentedon Nov 11, 2025
I also wonder whether completely disabling nitro plugin is good. For example, it might be nice if some nitro specific module convention (virtual module etc.) works inside Vitest.
(For the context, for example, sveltekit plugin can work with Vitest and it allows
$lib/.., $envetc. module convention.)pi0 commentedon Nov 18, 2025
/cc @juliusmarminge. You should disable plugin for test environment.
Probably best we do it out of the box until vitest support lands.
theoludwig commentedon Nov 23, 2025
When fixing this issue, it might also be worth to check support for Storybook as well: storybookjs/storybook#33010
dlukt commentedon Oct 1, 2026
Hi 👋 — adding another (distinct) manifestation of this issue with a full diagnosis, in case it helps pin the root cause.
Environment
@tanstack/react-start1.168.60, react 19.3.0nitro()vite plugin alongsidetanstackStart()Symptom
vitest runprints this on every run (tests still pass — it's console noise from the dev runner), plus theclose timed out after 10000ms/ hanging-process behavior from #4144:Bisected the plugin set: the error only appears when both
nitro()andtanstackStart()are present — each one alone is clean.Mechanism (traced via instrumentation)
dist/runtime/internal/vite/dev-worker.mjs) registers env runners — for this setup:nitro→nitro/dist/runtime/internal/vite/dev-entry.mjsssr→@tanstack/react-start/dist/plugin/default-entry/server.tsand eagerly imports each entry (
this.entry = await this.runner.import(this.entryPath)).ssrentry graph is:server.ts→@tanstack/react-start/server→@tanstack/react-start-server→StartServer.js, whose first line is a bareimport "react".react/index.js). By the time the runner requests it, the specifier is a resolved file path, and vite'sfetchModule()only takes the externalize branch for bare specifiers with an importer (url[0] !== "." && url[0] !== "/") — path-form URLs always go throughtransformRequestand get inlined. TheESModulesEvaluatorthen evaluates the CJS file as ESM →module is not defined. (resolve.external/ssr.externalare never consulted for these fetches, so they can't intercept it.)vite devthis doesn't happen because react-start addsoptimizeDeps.include: ["react", ...]for the server environments, so react is pre-bundled to ESM. Under vitest, react-start'sneedsOptimizeDepsis false (vitest setsoptimizeDeps.noDiscovery = true), so no pre-bundle happens and the raw CJS file is what reaches the runner.console.error(error); this.entryError = error), which is why it only surfaces as noise and doesn't fail the run.So it looks like the intersection of: nitro v3's dev runner eagerly evaluating the SSR entry + react-start disabling deps optimization under vitest (see also TanStack/router#6074, which is the flip side — it fixes dual-react-instance hook errors by skipping optimizeDeps in
VITEST).Reproduction
A vite config with just:
plus any passing test file, triggers it with
vitest run.vite devandvite buildare both unaffected (dev serves SSR fine, no errors).Workaround
Same as suggested in #4144 — skip the plugin under vitest:
Happy to provide a full standalone repro repo if useful. Thanks for the work on v3! 🙏
pi0x commentedon Oct 18, 2026
Vitest support landed in #4748, so the original report may be fixed on the latest beta. Could you check? The newer report above (CJS
reactinlined in thessrenv runner when using TanStack Start under Vitest) looks like a separate problem and might be easier to track in its own issue.Triaged with AI assistance.
pi0 commentedon Oct 18, 2026
(vitest support in next release and currently on v3 nightly)