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
43 changes: 32 additions & 11 deletions packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ export function createPool(ctx: Vitest): ProcessPool {
const environments = await getSpecificationsEnvironments(specs)
const groups = groupSpecs(sorted)

const projectEnvs = new WeakMap<TestProject, Partial<NodeJS.ProcessEnv>>()
const projectExecArgvs = new WeakMap<TestProject, string[]>()

for (const group of groups) {
if (!group) {
continue
Expand All @@ -114,6 +117,33 @@ export function createPool(ctx: Vitest): ProcessPool {
throw new Error(`Cannot find the environment. This is a bug in Vitest.`)
}

let env = projectEnvs.get(project)
if (!env) {
env = {
...process.env,
...options.env,
...ctx.config.env,
...project.config.env,
}

// env are case-insensitive on Windows, but spawned processes don't support it
if (isWindows) {
for (const name in env) {
env[name.toUpperCase()] = env[name]
}
}
projectEnvs.set(project, env)
}

let execArgv = projectExecArgvs.get(project)
if (!execArgv) {
execArgv = [
...options.execArgv,
...project.config.execArgv,
]
projectExecArgvs.set(project, execArgv)
}

taskGroup.push({
context: {
pool,
Expand All @@ -126,8 +156,8 @@ export function createPool(ctx: Vitest): ProcessPool {
workerId: workerId++,
},
project,
env: { ...options.env, ...project.config.env },
execArgv: [...options.execArgv, ...project.config.execArgv],
env,
execArgv,
worker: pool,
isolate: project.config.isolate,
memoryLimit: getMemoryLimit(ctx.config, pool) ?? null,
Expand Down Expand Up @@ -250,18 +280,9 @@ function resolveOptions(ctx: Vitest) {
NODE_ENV: process.env.NODE_ENV || 'test',
VITEST_MODE: ctx.config.watch ? 'WATCH' : 'RUN',
FORCE_TTY: isatty(1) ? 'true' : '',
...process.env,
...ctx.config.env,
},
}

// env are case-insensitive on Windows, but spawned processes don't support it
if (isWindows) {
for (const name in options.env) {
options.env[name.toUpperCase()] = options.env[name]
}
}

return options
}

Expand Down
8 changes: 8 additions & 0 deletions packages/vitest/src/node/pools/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ export interface PoolTask {
worker: 'forks' | 'threads' | 'vmForks' | 'vmThreads' | (string & {})
project: TestProject
isolate: boolean
/**
* Custom `process.env`. All tasks in the same project will reference the same object,
* so modifying it once will modify it for every task.
*/
env: Partial<NodeJS.ProcessEnv>
/**
* Custom `execArgv`. All tasks in the same project will reference the same array,
* so modifying it once will modify it for every task.
*/
execArgv: string[]
context: ContextRPC
memoryLimit: number | null
Expand Down
3 changes: 1 addition & 2 deletions packages/vitest/src/node/pools/workers/vmForksWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ export class VmForksPoolWorker extends ForksPoolWorker {
protected readonly entrypoint: string

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

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

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

/** Loads {@link file://./../../../runtime/workers/vmThreads.ts} */
this.entrypoint = resolve(options.distPath, 'workers/vmThreads.js')
Expand Down
3 changes: 3 additions & 0 deletions test/global-setup/globalSetup/update-env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => {
process.env.NEW_VALUE = 'true'
}
4 changes: 4 additions & 0 deletions test/global-setup/test/setup-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ test('setup file has been loaded without relative path prefix', () => {
const result = loaded
expect(result).toBeTruthy()
})

test('the process.env is injected correctly', () => {
expect(process.env.NEW_VALUE).toBe('true')
})
1 change: 1 addition & 0 deletions test/global-setup/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default defineConfig({
'./globalSetup/named-exports.js',
'./globalSetup/ts-with-imports.ts',
'./globalSetup/another-vite-instance.ts',
'./globalSetup/update-env.ts',
],
},
})
Loading