Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/guide/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ import type { Environment } from 'vitest/runtime'
export default <Environment>{
name: 'custom',
viteEnvironment: 'ssr',
// optional - set to false when "setupVM" is fast, so vm pools
// do not transform the import graph while it runs
prewarmModules: true,
// optional - only if you support "vmForks" or "vmThreads" pools
async setupVM() {
const vm = await import('node:vm')
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/integrations/env/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function populateNodeGlobals() {
export default <Environment>{
name: 'node',
viteEnvironment: 'ssr',
prewarmModules: false,
// this is largely copied from jest's node environment
async setupVM() {
populateNodeGlobals()
Expand Down
17 changes: 8 additions & 9 deletions packages/vitest/src/runtime/workers/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ export async function runVmTests(method: 'run' | 'collect', state: WorkerGlobalS
state.environment = environment

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

if (!environment.setupVM) {
const envName = ctx.environment.name
Expand Down
8 changes: 8 additions & 0 deletions packages/vitest/src/types/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export interface Environment {
* By default, fallbacks to `name`.
*/
viteEnvironment?: 'client' | 'ssr' | ({} & string)
/**
* Let the server transform the test file's import graph while `setupVM`
* runs. Only worth it when `setupVM` is slow (jsdom, happy-dom): otherwise
* the transforms compete with the worker's own module requests.
*
* @default `true`
*/
prewarmModules?: boolean
setupVM?: (options: Record<string, any>) => Awaitable<VmEnvironmentReturn>
setup: (
global: any,
Expand Down
Loading