Skip to content

Commit a6d5ea2

Browse files
authored
perf(vm): let environments opt out of the module graph prewarm (#11078)
1 parent 91be1d7 commit a6d5ea2

4 files changed

Lines changed: 20 additions & 9 deletions

File tree

docs/guide/environment.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ import type { Environment } from 'vitest/runtime'
4949
export default <Environment>{
5050
name: 'custom',
5151
viteEnvironment: 'ssr',
52+
// optional - set to false when "setupVM" is fast, so vm pools
53+
// do not transform the import graph while it runs
54+
prewarmModules: true,
5255
// optional - only if you support "vmForks" or "vmThreads" pools
5356
async setupVM() {
5457
const vm = await import('node:vm')

packages/vitest/src/integrations/env/node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ function populateNodeGlobals() {
4141
export default <Environment>{
4242
name: 'node',
4343
viteEnvironment: 'ssr',
44+
prewarmModules: false,
4445
// this is largely copied from jest's node environment
4546
async setupVM() {
4647
populateNodeGlobals()

packages/vitest/src/runtime/workers/vm.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ export async function runVmTests(method: 'run' | 'collect', state: WorkerGlobalS
3737
state.environment = environment
3838

3939
// let the server transform this file's import graph while this worker is
40-
// busy importing the environment package (jsdom takes ~0.5s per worker) —
41-
// the server is otherwise idle during that window on a cold start. The
42-
// transforms also land in the `fetchWarmModules` snapshot, so the worker's
43-
// own fetches short-circuit to disk reads. Failures are ignored: the
44-
// worker's own fetch reports them with the proper import context.
45-
rpc.prewarmModuleGraph(
46-
environment.viteEnvironment || environment.name,
47-
ctx.files.map(file => file.filepath),
48-
).catch(() => {})
40+
// busy setting up the environment (jsdom takes ~0.5s per worker) —
41+
// the server is otherwise idle during that window on a cold start.
42+
if (environment.prewarmModules !== false) {
43+
rpc.prewarmModuleGraph(
44+
environment.viteEnvironment || environment.name,
45+
ctx.files.map(file => file.filepath),
46+
).catch(() => {})
47+
}
4948

5049
if (!environment.setupVM) {
5150
const envName = ctx.environment.name

packages/vitest/src/types/environment.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ export interface Environment {
2222
* By default, fallbacks to `name`.
2323
*/
2424
viteEnvironment?: 'client' | 'ssr' | ({} & string)
25+
/**
26+
* Let the server transform the test file's import graph while `setupVM`
27+
* runs. Only worth it when `setupVM` is slow (jsdom, happy-dom): otherwise
28+
* the transforms compete with the worker's own module requests.
29+
*
30+
* @default `true`
31+
*/
32+
prewarmModules?: boolean
2533
setupVM?: (options: Record<string, any>) => Awaitable<VmEnvironmentReturn>
2634
setup: (
2735
global: any,

0 commit comments

Comments
 (0)