Describe the bug
When the pool is vmThreads (or vmForks), Vite's dependency optimizer runs over the project's index.html import graph and pre-bundles what it finds. If a dependency reached from index.html cannot be bundled, the run ends with an unhandled rejection and exit code 1, even when every test passes and no test imports that dependency. With pool: 'threads' the same project exits 0.
Our case: an app whose index.html entry reaches react-native (Flow source) via react-native-web. Every test passed under threads; under vmThreads the run aborted with [PARSE_ERROR] Flow is not supported from react-native/Libraries/Utilities/codegenNativeComponent.js.
From reading dist/chunks/cli-api-*.js: a vm pool adds the __vitest_vm__ Vite environment (consumer: "client"), and the configEnvironment hook returns early for it (if (name === "__vitest_vm__" || name === "__vitest__") return;). So it never gets the resolveOptimizerConfig(...) override the other environments get, and it keeps Vite's client default: discover dependencies from every index.html. Since vm tests load modules through native import, it looks like the optimizer has no work to do in that environment at all.
Setting optimizeDeps.noDiscovery at the top level does not help. Setting it on the environment does:
environments: { __vitest_vm__: { optimizeDeps: { noDiscovery: true } } },
That is our current workaround, but it depends on an internal environment name.
Reproduction
https://github.com/iKyi/vitest-vm-repros/tree/main/optimizer
index.html loads main.js, which imports flowdep (a local package with Flow annotations, installed as a real package via .npmrc install-links=true). The only test is expect(1).toBe(1).
npm install
npx vitest run # config pool vmThreads: all tests pass, exit 1
npx vitest run --pool=threads # exit 0
Unhandled Rejection
Error: Error during dependency optimization:
Build failed with 1 error:
[PARSE_ERROR] Flow is not supported
| variant |
exit |
pool vmThreads |
1 |
pool threads |
0 |
vmThreads + top-level optimizeDeps: { noDiscovery: true } |
1 |
vmThreads + environments.__vitest_vm__.optimizeDeps.noDiscovery |
0 |
vmThreads, index.html removed |
0 |
System Info
System:
OS: Linux 7.0 Ubuntu 26.04.1 LTS
CPU: (32) x64 AMD RYZEN AI MAX+ 395 w/ Radeon 8060S
Binaries:
Node: 22.22.3
npm: 10.9.8
npmPackages:
happy-dom: 20.14.5
vitest: 5.0.1
vite: 8.3.0
rolldown: 1.2.9
Used Package Manager
npm
Validations
Describe the bug
When the pool is
vmThreads(orvmForks), Vite's dependency optimizer runs over the project'sindex.htmlimport graph and pre-bundles what it finds. If a dependency reached fromindex.htmlcannot be bundled, the run ends with an unhandled rejection and exit code 1, even when every test passes and no test imports that dependency. Withpool: 'threads'the same project exits 0.Our case: an app whose
index.htmlentry reachesreact-native(Flow source) via react-native-web. Every test passed underthreads; undervmThreadsthe run aborted with[PARSE_ERROR] Flow is not supportedfromreact-native/Libraries/Utilities/codegenNativeComponent.js.From reading
dist/chunks/cli-api-*.js: a vm pool adds the__vitest_vm__Vite environment (consumer: "client"), and theconfigEnvironmenthook returns early for it (if (name === "__vitest_vm__" || name === "__vitest__") return;). So it never gets theresolveOptimizerConfig(...)override the other environments get, and it keeps Vite's client default: discover dependencies from everyindex.html. Since vm tests load modules through native import, it looks like the optimizer has no work to do in that environment at all.Setting
optimizeDeps.noDiscoveryat the top level does not help. Setting it on the environment does:That is our current workaround, but it depends on an internal environment name.
Reproduction
https://github.com/iKyi/vitest-vm-repros/tree/main/optimizer
index.htmlloadsmain.js, which importsflowdep(a local package with Flow annotations, installed as a real package via.npmrc install-links=true). The only test isexpect(1).toBe(1).vmThreadsthreadsvmThreads+ top-leveloptimizeDeps: { noDiscovery: true }vmThreads+environments.__vitest_vm__.optimizeDeps.noDiscoveryvmThreads,index.htmlremovedSystem Info
Used Package Manager
npm
Validations