File tree Expand file tree Collapse file tree 6 files changed +24
-10
lines changed
vitest-environment-custom Expand file tree Collapse file tree 6 files changed +24
-10
lines changed Original file line number Diff line number Diff line change @@ -73,6 +73,9 @@ export class Pool {
7373 let isMemoryLimitReached = false
7474 const runner = this . getPoolRunner ( task , method )
7575
76+ const poolId = runner . poolId ?? this . getWorkerId ( )
77+ runner . poolId = poolId
78+
7679 const activeTask = { task, resolver, method, cancelTask }
7780 this . activeTasks . push ( activeTask )
7881
@@ -110,12 +113,9 @@ export class Pool {
110113 WORKER_START_TIMEOUT ,
111114 )
112115
113- await runner . start ( ) . finally ( ( ) => clearTimeout ( id ) )
116+ await runner . start ( { workerId : task . context . workerId } ) . finally ( ( ) => clearTimeout ( id ) )
114117 }
115118
116- const poolId = runner . poolId ?? this . getWorkerId ( )
117- runner . poolId = poolId
118-
119119 const span = runner . startTracesSpan ( `vitest.worker.${ method } ` )
120120 // Start running the test in the worker
121121 runner . request ( method , task . context )
Original file line number Diff line number Diff line change @@ -127,7 +127,6 @@ export class PoolRunner {
127127 __vitest_worker_request__ : true ,
128128 type : method ,
129129 context,
130- poolId : this . poolId ! ,
131130 otelCarrier : this . getOTELCarrier ( ) ,
132131 } satisfies WorkerRequest )
133132 }
@@ -139,7 +138,7 @@ export class PoolRunner {
139138 : undefined
140139 }
141140
142- async start ( ) : Promise < void > {
141+ async start ( options : { workerId : number } ) : Promise < void > {
143142 // Wait for any ongoing operation to complete
144143 if ( this . _operationLock ) {
145144 await this . _operationLock
@@ -182,6 +181,8 @@ export class PoolRunner {
182181
183182 this . postMessage ( {
184183 type : 'start' ,
184+ poolId : this . poolId ! ,
185+ workerId : options . workerId ,
185186 __vitest_worker_request__ : true ,
186187 options : {
187188 reportMemory : this . worker . reportMemory ?? false ,
Original file line number Diff line number Diff line change @@ -70,6 +70,8 @@ export type WorkerRequest
7070 = { __vitest_worker_request__ : true } & (
7171 | {
7272 type : 'start'
73+ poolId : number
74+ workerId : WorkerExecuteContext [ 'workerId' ] // Initial worker ID, may change when non-isolated worker runs multiple test files
7375 options : { reportMemory : boolean }
7476 context : {
7577 environment : WorkerTestEnvironment
@@ -89,13 +91,11 @@ export type WorkerRequest
8991 | {
9092 type : 'run'
9193 context : WorkerExecuteContext
92- poolId : number
9394 otelCarrier ?: OTELCarrier
9495 }
9596 | {
9697 type : 'collect'
9798 context : WorkerExecuteContext
98- poolId : number
9999 otelCarrier ?: OTELCarrier
100100 }
101101 | { type : 'cancel' }
Original file line number Diff line number Diff line change @@ -41,6 +41,8 @@ export function init(worker: Options): void {
4141
4242 switch ( message . type ) {
4343 case 'start' : {
44+ process . env . VITEST_POOL_ID = String ( message . poolId )
45+ process . env . VITEST_WORKER_ID = String ( message . workerId )
4446 reportMemory = message . options . reportMemory
4547
4648 const tracesStart = performance . now ( )
@@ -96,7 +98,6 @@ export function init(worker: Options): void {
9698 }
9799
98100 try {
99- process . env . VITEST_POOL_ID = String ( message . poolId )
100101 process . env . VITEST_WORKER_ID = String ( message . context . workerId )
101102 }
102103 catch ( error ) {
@@ -155,7 +156,6 @@ export function init(worker: Options): void {
155156 }
156157
157158 try {
158- process . env . VITEST_POOL_ID = String ( message . poolId )
159159 process . env . VITEST_WORKER_ID = String ( message . context . workerId )
160160 }
161161 catch ( error ) {
Original file line number Diff line number Diff line change @@ -6,4 +6,12 @@ test('custom env is defined', () => {
66 expect ( expect . getState ( ) . environment ) . toBe ( 'custom' )
77 expect ( ( globalThis as any ) . testEnvironment ) . toBe ( 'custom' )
88 expect ( ( globalThis as any ) . option ) . toBe ( 'config-option' )
9+
10+ expect ( ( global as any ) . POOL_ID_DURING_ENV_SETUP ) . toBeDefined ( )
11+ expect ( process . env . VITEST_POOL_ID ) . toBeDefined ( )
12+ expect ( ( global as any ) . POOL_ID_DURING_ENV_SETUP ) . toBe ( process . env . VITEST_POOL_ID )
13+
14+ expect ( ( global as any ) . WORKER_ID_DURING_ENV_SETUP ) . toBeDefined ( )
15+ expect ( process . env . VITEST_WORKER_ID ) . toBeDefined ( )
16+ expect ( ( global as any ) . WORKER_ID_DURING_ENV_SETUP ) . toBe ( process . env . VITEST_WORKER_ID )
917} )
Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ export default <Environment>{
1212 const context = vm . createContext ( {
1313 testEnvironment : 'custom' ,
1414 option : custom . option ,
15+ POOL_ID_DURING_ENV_SETUP : process . env . VITEST_POOL_ID ,
16+ WORKER_ID_DURING_ENV_SETUP : process . env . VITEST_WORKER_ID ,
1517 setTimeout,
1618 clearTimeout,
1719 AbortController,
@@ -31,6 +33,9 @@ export default <Environment>{
3133 setup ( global , { custom } ) {
3234 global . testEnvironment = 'custom'
3335 global . option = custom . option
36+ global . POOL_ID_DURING_ENV_SETUP = process . env . VITEST_POOL_ID
37+ global . WORKER_ID_DURING_ENV_SETUP = process . env . VITEST_WORKER_ID
38+
3439 return {
3540 teardown ( ) {
3641 delete global . testEnvironment
You can’t perform that action at this time.
0 commit comments