nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Vitest working with Nitro v2, but not with Nitro v3.0.1-alpha.0

Open theoludwig opened this issue 2 months ago • 9 comments

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.3

Reproduction

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-plugin and 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 with vitest 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/[email protected][email protected]_@[email protected][email protected][email protected][email protected]_/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/[email protected]_@[email protected][email protected][email protected][email protected]/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/[email protected]_@[email protected][email protected][email protected][email protected]/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/[email protected][email protected]_@[email protected][email protected][email protected][email protected]_/node_modules/nitro/dist/_chunks/plugin.mjs:318:5)
    at createFetchableDevEnvironment (file:///home/runner/work/vite-nitro-issues/vite-nitro-issues/node_modules/.pnpm/[email protected][email protected]_@[email protected][email protected][email protected][email protected]_/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/[email protected][email protected]_@[email protected][email protected][email protected][email protected]_/node_modules/nitro/dist/_chunks/plugin.mjs:525:50)
    at _createServer (file:///home/runner/work/vite-nitro-issues/vite-nitro-issues/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/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/[email protected]_@[email protected]_@[email protected][email protected][email protected][email protected]/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/[email protected]_@[email protected]_@[email protected][email protected][email protected][email protected]/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/[email protected]_@[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:10541:14)

theoludwig avatar Oct 14 '25 20:10 theoludwig

For unit testing, server runtime is irrelevant, so nitro should be disabled. One quick way is to skip the plugin based on process.env.VITEST such as plugins: [!process.env.VITEST && nitro()]. Tanstack start's Nitro v2 plugin was essentially build only, so it didn't manifest any issues.

hi-ogawa avatar Oct 17 '25 11:10 hi-ogawa

For unit testing, server runtime is irrelevant, so nitro should be disabled. One quick way is to skip the plugin based on process.env.VITEST such as plugins: [!process.env.VITEST && nitro()]. Tanstack start's Nitro v2 plugin was essentially build only, so it didn't manifest any issues.

Ah, okay, it makes sense, thanks for the reply. I tried it, and indeed it seems to work: https://github.com/theoludwig/vite-nitro-issues/commit/8255b85de5f6b36c8455f063e10611adb89c19dc

theoludwig avatar Oct 18 '25 17:10 theoludwig

reopening issue to try to make nitro plugin behavior smarter when running within tests.

pi0 avatar Oct 19 '25 17:10 pi0

behavior smarter when running within tests.

Means Nitro could skip internally when detected running with tests? If that's it, I can help. 🙌

kricsleo avatar Oct 21 '25 08:10 kricsleo

behavior smarter when running within tests.

Means Nitro could skip internally when detected running with tests? If that's it, I can help. 🙌

Is the above could be a solution? @pi0

For now users, must have in their vite.config.ts the following:

import { nitro } from "nitro/vite"
import { defineConfig } from "vite"

const IS_TEST = process.env["NODE_ENV"] === "test"

export default defineConfig({
  plugins: [
    ...(!IS_TEST ? [nitro()] : []),
  ],
})

theoludwig avatar Nov 10 '25 19:11 theoludwig

@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.

pi0 avatar Nov 10 '25 19:11 pi0

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/.., $env etc. module convention.)

hi-ogawa avatar Nov 11 '25 01:11 hi-ogawa

/cc @juliusmarminge. You should disable plugin for test environment.

Probably best we do it out of the box until vitest support lands.

pi0 avatar Nov 18 '25 19:11 pi0

When fixing this issue, it might also be worth to check support for Storybook as well: https://github.com/storybookjs/storybook/issues/33010

theoludwig avatar Nov 23 '25 18:11 theoludwig