| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -89,6 +89,9 @@ export function createPool(ctx: Vitest): ProcessPool { | |||
| 89 | 89 | const environments = await getSpecificationsEnvironments(specs) | |
| 90 | 90 | const groups = groupSpecs(sorted) | |
| 91 | 91 | ||
| 92 | + const projectEnvs = new WeakMap<TestProject, Partial<NodeJS.ProcessEnv>>() | ||
| 93 | + const projectExecArgvs = new WeakMap<TestProject, string[]>() | ||
| 94 | + | ||
| 92 | 95 | for (const group of groups) { | |
| 93 | 96 | if (!group) { | |
| 94 | 97 | continue | |
@@ -114,6 +117,33 @@ export function createPool(ctx: Vitest): ProcessPool { | |||
| 114 | 117 | throw new Error(`Cannot find the environment. This is a bug in Vitest.`) | |
| 115 | 118 | } | |
| 116 | 119 | ||
| 120 | + let env = projectEnvs.get(project) | ||
| 121 | + if (!env) { | ||
| 122 | + env = { | ||
| 123 | + ...process.env, | ||
| 124 | + ...options.env, | ||
| 125 | + ...ctx.config.env, | ||
| 126 | + ...project.config.env, | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + // env are case-insensitive on Windows, but spawned processes don't support it | ||
| 130 | + if (isWindows) { | ||
| 131 | + for (const name in env) { | ||
| 132 | + env[name.toUpperCase()] = env[name] | ||
| 133 | + } | ||
| 134 | + } | ||
| 135 | + projectEnvs.set(project, env) | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + let execArgv = projectExecArgvs.get(project) | ||
| 139 | + if (!execArgv) { | ||
| 140 | + execArgv = [ | ||
| 141 | + ...options.execArgv, | ||
| 142 | + ...project.config.execArgv, | ||
| 143 | + ] | ||
| 144 | + projectExecArgvs.set(project, execArgv) | ||
| 145 | + } | ||
| 146 | + | ||
| 117 | 147 | taskGroup.push({ | |
| 118 | 148 | context: { | |
| 119 | 149 | pool, | |
@@ -126,8 +156,8 @@ export function createPool(ctx: Vitest): ProcessPool { | |||
| 126 | 156 | workerId: workerId++, | |
| 127 | 157 | }, | |
| 128 | 158 | project, | |
| 129 | - env: { ...options.env, ...project.config.env }, | ||
| 130 | - execArgv: [...options.execArgv, ...project.config.execArgv], | ||
| 159 | + env, | ||
| 160 | + execArgv, | ||
| 131 | 161 | worker: pool, | |
| 132 | 162 | isolate: project.config.isolate, | |
| 133 | 163 | memoryLimit: getMemoryLimit(ctx.config, pool) ?? null, | |
@@ -250,18 +280,9 @@ function resolveOptions(ctx: Vitest) { | |||
| 250 | 280 | NODE_ENV: process.env.NODE_ENV || 'test', | |
| 251 | 281 | VITEST_MODE: ctx.config.watch ? 'WATCH' : 'RUN', | |
| 252 | 282 | FORCE_TTY: isatty(1) ? 'true' : '', | |
| 253 | - ...process.env, | ||
| 254 | - ...ctx.config.env, | ||
| 255 | 283 | }, | |
| 256 | 284 | } | |
| 257 | 285 | ||
| 258 | - // env are case-insensitive on Windows, but spawned processes don't support it | ||
| 259 | - if (isWindows) { | ||
| 260 | - for (const name in options.env) { | ||
| 261 | - options.env[name.toUpperCase()] = options.env[name] | ||
| 262 | - } | ||
| 263 | - } | ||
| 264 | - | ||
| 265 | 286 | return options | |
| 266 | 287 | } | |
| 267 | 288 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,7 +41,15 @@ export interface PoolTask { | |||
| 41 | 41 | worker: 'forks' | 'threads' | 'vmForks' | 'vmThreads' | (string & {}) | |
| 42 | 42 | project: TestProject | |
| 43 | 43 | isolate: boolean | |
| 44 | + /** | ||
| 45 | + * Custom `process.env`. All tasks in the same project will reference the same object, | ||
| 46 | + * so modifying it once will modify it for every task. | ||
| 47 | + */ | ||
| 44 | 48 | env: Partial<NodeJS.ProcessEnv> | |
| 49 | + /** | ||
| 50 | + * Custom `execArgv`. All tasks in the same project will reference the same array, | ||
| 51 | + * so modifying it once will modify it for every task. | ||
| 52 | + */ | ||
| 45 | 53 | execArgv: string[] | |
| 46 | 54 | context: ContextRPC | |
| 47 | 55 | memoryLimit: number | null | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,8 +9,7 @@ export class VmForksPoolWorker extends ForksPoolWorker { | |||
| 9 | 9 | protected readonly entrypoint: string | |
| 10 | 10 | ||
| 11 | 11 | constructor(options: PoolOptions) { | |
| 12 | - super(options) | ||
| 13 | - this.execArgv.push('--experimental-vm-modules') | ||
| 12 | + super({ ...options, execArgv: [...options.execArgv, '--experimental-vm-modules'] }) | ||
| 14 | 13 | ||
| 15 | 14 | /** Loads {@link file://./../../../runtime/workers/vmForks.ts} */ | |
| 16 | 15 | this.entrypoint = resolve(options.distPath, 'workers/vmForks.js') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,8 +9,7 @@ export class VmThreadsPoolWorker extends ThreadsPoolWorker { | |||
| 9 | 9 | protected readonly entrypoint: string | |
| 10 | 10 | ||
| 11 | 11 | constructor(options: PoolOptions) { | |
| 12 | - super(options) | ||
| 13 | - this.execArgv.push('--experimental-vm-modules') | ||
| 12 | + super({ ...options, execArgv: [...options.execArgv, '--experimental-vm-modules'] }) | ||
| 14 | 13 | ||
| 15 | 14 | /** Loads {@link file://./../../../runtime/workers/vmThreads.ts} */ | |
| 16 | 15 | this.entrypoint = resolve(options.distPath, 'workers/vmThreads.js') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + export default () => { | ||
| 2 | + process.env.NEW_VALUE = 'true' | ||
| 3 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,3 +11,7 @@ test('setup file has been loaded without relative path prefix', () => { | |||
| 11 | 11 | const result = loaded | |
| 12 | 12 | expect(result).toBeTruthy() | |
| 13 | 13 | }) | |
| 14 | + | ||
| 15 | + test('the process.env is injected correctly', () => { | ||
| 16 | + expect(process.env.NEW_VALUE).toBe('true') | ||
| 17 | + }) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,6 +20,7 @@ export default defineConfig({ | |||
| 20 | 20 | './globalSetup/named-exports.js', | |
| 21 | 21 | './globalSetup/ts-with-imports.ts', | |
| 22 | 22 | './globalSetup/another-vite-instance.ts', | |
| 23 | + './globalSetup/update-env.ts', | ||
| 23 | 24 | ], | |
| 24 | 25 | }, | |
| 25 | 26 | }) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments