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
2 changes: 1 addition & 1 deletion packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function createPool(ctx: Vitest): ProcessPool {
workerId: workerId++,
},
project,
env: options.env,
env: { ...options.env, ...project.config.env },
execArgv: [...options.execArgv, ...project.config.execArgv],
worker: pool,
isolate: project.config.isolate,
Expand Down
13 changes: 1 addition & 12 deletions packages/vitest/src/node/pools/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,6 @@ function isEqualRunner(runner: PoolRunner, task: PoolTask) {
runner.worker.name === task.worker
&& runner.project === task.project
&& runner.environment === task.context.environment.name
&& runner.worker.execArgv.every((arg, index) => task.execArgv[index] === arg)
&& isEnvEqual(runner.worker.env, task.env)
&& (!runner.worker.canReuse || runner.worker.canReuse(task))
)
}

function isEnvEqual(a: PoolOptions['env'], b: PoolTask['env']) {
const keys = Object.keys(a)

if (keys.length !== Object.keys(b).length) {
return false
}

return keys.every(key => a[key] === b[key])
}
13 changes: 9 additions & 4 deletions packages/vitest/src/node/pools/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ export interface PoolOptions {
cacheFs?: boolean
environment: string
execArgv: string[]
env: Record<string, string>
env: Partial<NodeJS.ProcessEnv>
}

export interface PoolWorker {
readonly name: string
readonly execArgv: string[]
readonly env: Record<string, string>
readonly reportMemory?: boolean
readonly cacheFs?: boolean

Expand All @@ -30,13 +28,20 @@ export interface PoolWorker {

start: () => Promise<void>
stop: () => Promise<void>

/**
* This is called on workers that already satisfy certain constraints:
* - The task has the same project
* - The task has the same environment
*/
canReuse?: (task: PoolTask) => boolean
}

export interface PoolTask {
worker: 'forks' | 'threads' | 'vmForks' | 'vmThreads' | (string & {})
project: TestProject
isolate: boolean
env: Record<string, string>
env: Partial<NodeJS.ProcessEnv>
execArgv: string[]
context: ContextRPC
memoryLimit: number | null
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/pools/workers/forksWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const SIGKILL_TIMEOUT = 500 /** jest does 500ms by default, let's follow it */
/** @experimental */
export class ForksPoolWorker implements PoolWorker {
public readonly name: string = 'forks'
public readonly execArgv: string[]
public readonly env: Record<string, string>
public readonly cacheFs: boolean = true

protected readonly entrypoint: string
protected execArgv: string[]
protected env: Partial<NodeJS.ProcessEnv>

private _fork?: ChildProcess

Expand Down
5 changes: 3 additions & 2 deletions packages/vitest/src/node/pools/workers/threadsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { Worker } from 'node:worker_threads'
/** @experimental */
export class ThreadsPoolWorker implements PoolWorker {
public readonly name: string = 'threads'
public readonly execArgv: string[]
public readonly env: Record<string, string>

protected readonly entrypoint: string
protected execArgv: string[]
protected env: Partial<NodeJS.ProcessEnv>

private _thread?: Worker

Expand Down
8 changes: 4 additions & 4 deletions packages/vitest/src/node/pools/workers/typecheckWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ import { Typechecker } from '../../../typecheck/typechecker'
/** @experimental */
export class TypecheckPoolWorker implements PoolWorker {
public readonly name: string = 'typecheck'
public readonly execArgv: string[]
public readonly env: Record<string, string>
private readonly project: TestProject

private _eventEmitter = new EventEmitter()

constructor(options: PoolOptions) {
this.execArgv = options.execArgv
this.env = options.env
this.project = options.project
}

Expand All @@ -33,6 +29,10 @@ export class TypecheckPoolWorker implements PoolWorker {
// noop, onMessage handles it
}

canReuse(): boolean {
return true
}

send(message: WorkerRequest): void {
void onMessage(message, this.project).then((response) => {
if (response) {
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/node/pools/workers/vmForksWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export class VmForksPoolWorker extends ForksPoolWorker {
protected readonly entrypoint: string

constructor(options: PoolOptions) {
super({ ...options, execArgv: [...options.execArgv, '--experimental-vm-modules'] })
super(options)
this.execArgv.push('--experimental-vm-modules')

/** Loads {@link file://./../../../runtime/workers/vmForks.ts} */
this.entrypoint = resolve(options.distPath, 'workers/vmForks.js')
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/node/pools/workers/vmThreadsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export class VmThreadsPoolWorker extends ThreadsPoolWorker {
protected readonly entrypoint: string

constructor(options: PoolOptions) {
super({ ...options, execArgv: [...options.execArgv, '--experimental-vm-modules'] })
super(options)
this.execArgv.push('--experimental-vm-modules')

/** Loads {@link file://./../../../runtime/workers/vmThreads.ts} */
this.entrypoint = resolve(options.distPath, 'workers/vmThreads.js')
Expand Down
4 changes: 0 additions & 4 deletions test/cli/fixtures/custom-pool/pool/custom-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ export class CustomRuntimeWorker implements PoolWorker {
public readonly name = 'custom'
private vitest: Vitest
private customEvents = new EventEmitter()
readonly execArgv: string[]
readonly env: Record<string, string>
private project: TestProject

constructor(options: PoolOptions, private settings: OptionsCustomPool) {
this.execArgv = options.execArgv
this.env = options.env
this.vitest = options.project.vitest
this.project = options.project
}
Expand Down
Loading