/
pool.ts
59 lines (51 loc) · 1.76 KB
/
pool.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { pathToFileURL } from 'node:url'
import { resolve } from 'pathe'
import { distDir, rootDir } from '../constants'
import type { Vitest } from './core'
import { createChildProcessPool } from './pools/child'
import { createThreadsPool } from './pools/threads'
export type RunWithFiles = (files: string[], invalidates?: string[]) => Promise<void>
export interface ProcessPool {
runTests: RunWithFiles
close: () => Promise<void>
}
export interface PoolProcessOptions {
execArgv: string[]
env: Record<string, string>
}
const loaderPath = pathToFileURL(resolve(distDir, './loader.js')).href
const suppressLoaderWarningsPath = resolve(rootDir, './suppress-warnings.cjs')
export function createPool(ctx: Vitest): ProcessPool {
const conditions = ctx.server.config.resolve.conditions?.flatMap(c => ['--conditions', c]) || []
// Instead of passing whole process.execArgv to the workers, pick allowed options.
// Some options may crash worker, e.g. --prof, --title. nodejs/node#41103
const execArgv = process.execArgv.filter(execArg =>
execArg.startsWith('--cpu-prof') || execArg.startsWith('--heap-prof'),
)
const options: PoolProcessOptions = {
execArgv: ctx.config.deps.registerNodeLoader
? [
...execArgv,
'--require',
suppressLoaderWarningsPath,
'--experimental-loader',
loaderPath,
...execArgv,
]
: [
...execArgv,
...conditions,
],
env: {
TEST: 'true',
VITEST: 'true',
NODE_ENV: ctx.config.mode || 'test',
VITEST_MODE: ctx.config.watch ? 'WATCH' : 'RUN',
...process.env,
...ctx.config.env,
},
}
if (!ctx.config.threads)
return createChildProcessPool(ctx, options)
return createThreadsPool(ctx, options)
}