@@ -36,6 +36,8 @@ export async function run(
3636 Object . defineProperty ( globalThis , '__vitest_index__' , {
3737 value : VitestIndex ,
3838 enumerable : false ,
39+ configurable : true ,
40+ writable : true ,
3941 } )
4042
4143 const viteEnvironment = workerState . environment . viteEnvironment || workerState . environment . name
@@ -79,7 +81,10 @@ export async function run(
7981
8082 config . snapshotOptions . snapshotEnvironment = snapshotEnvironment
8183
82- workerState . onCancel ( ( reason ) => {
84+ // the callback captures this file's runner: unsubscribe once the run is
85+ // over, or every finished file's world stays reachable from the worker's
86+ // cancel listeners for the lifetime of the worker
87+ const offCancel = workerState . onCancel ( ( reason ) => {
8388 closeInspector ( config )
8489 testRunner . cancel ?.( reason )
8590 } )
@@ -89,42 +94,47 @@ export async function run(
8994
9095 const { vi } = VitestIndex
9196
92- await traces . $ (
93- `vitest.test.runner.${ method } ` ,
94- async ( ) => {
95- for ( const file of files ) {
96- workerState . filepath = file . filepath
97+ try {
98+ await traces . $ (
99+ `vitest.test.runner.${ method } ` ,
100+ async ( ) => {
101+ for ( const file of files ) {
102+ workerState . filepath = file . filepath
97103
98- if ( method === 'run' ) {
99- const collectAsyncLeaks = config . detectAsyncLeaks ? detectAsyncLeaks ( file . filepath , workerState . ctx . projectName ) : undefined
104+ if ( method === 'run' ) {
105+ const collectAsyncLeaks = config . detectAsyncLeaks ? detectAsyncLeaks ( file . filepath , workerState . ctx . projectName ) : undefined
100106
101- await traces . $ (
102- `vitest.test.runner.${ method } .module` ,
103- { attributes : { 'code.file.path' : file . filepath } } ,
104- ( ) => startTests ( [ file ] , testRunner ) ,
105- )
107+ await traces . $ (
108+ `vitest.test.runner.${ method } .module` ,
109+ { attributes : { 'code.file.path' : file . filepath } } ,
110+ ( ) => startTests ( [ file ] , testRunner ) ,
111+ )
106112
107- const leaks = await collectAsyncLeaks ?.( )
113+ const leaks = await collectAsyncLeaks ?.( )
108114
109- if ( leaks ?. length ) {
110- workerState . rpc . onAsyncLeaks ( leaks )
115+ if ( leaks ?. length ) {
116+ workerState . rpc . onAsyncLeaks ( leaks )
117+ }
118+ }
119+ else {
120+ await traces . $ (
121+ `vitest.test.runner.${ method } .module` ,
122+ { attributes : { 'code.file.path' : file . filepath } } ,
123+ ( ) => collectTests ( [ file ] , testRunner ) ,
124+ )
111125 }
112- }
113- else {
114- await traces . $ (
115- `vitest.test.runner.${ method } .module` ,
116- { attributes : { 'code.file.path' : file . filepath } } ,
117- ( ) => collectTests ( [ file ] , testRunner ) ,
118- )
119- }
120126
121- // reset after tests, because user might call `vi.setConfig` in setupFile
122- vi . resetConfig ( )
123- // mocks should not affect different files
124- vi . restoreAllMocks ( )
125- }
126- } ,
127- )
127+ // reset after tests, because user might call `vi.setConfig` in setupFile
128+ vi . resetConfig ( )
129+ // mocks should not affect different files
130+ vi . restoreAllMocks ( )
131+ }
132+ } ,
133+ )
134+ }
135+ finally {
136+ offCancel ( )
137+ }
128138
129139 await traces . $ ( 'vitest.runtime.coverage.stop' , ( ) => stopCoverageInsideWorker ( config . coverage , moduleRunner , { isolate : false } ) )
130140}
0 commit comments