@@ -42,14 +42,11 @@ export interface ContextExecutorOptions {
4242
4343const bareVitestRegexp = / ^ @ ? v i t e s t ( \/ | $ ) /
4444
45- export async function startVitestExecutor ( options : ContextExecutorOptions ) {
46- // @ts -expect-error injected untyped global
47- const state = ( ) : WorkerGlobalState => globalThis . __vitest_worker__ || options . state
48- const rpc = ( ) => state ( ) . rpc
45+ const dispose : ( ( ) => void ) [ ] = [ ]
4946
50- process . exit = ( code = process . exitCode || 0 ) : never => {
51- throw new Error ( `process.exit unexpectedly called with " ${ code } "` )
52- }
47+ function listenForErrors ( state : ( ) => WorkerGlobalState ) {
48+ dispose . forEach ( fn => fn ( ) )
49+ dispose . length = 0
5350
5451 function catchError ( err : unknown , type : string ) {
5552 const worker = state ( )
@@ -60,13 +57,31 @@ export async function startVitestExecutor(options: ContextExecutorOptions) {
6057 error . VITEST_TEST_PATH = relative ( state ( ) . config . root , worker . filepath )
6158 error . VITEST_AFTER_ENV_TEARDOWN = worker . environmentTeardownRun
6259 }
63- rpc ( ) . onUnhandledError ( error , type )
60+ state ( ) . rpc . onUnhandledError ( error , type )
6461 }
6562
66- process . setMaxListeners ( 25 )
63+ const uncaughtException = ( e : Error ) => catchError ( e , 'Uncaught Exception' )
64+ const unhandledRejection = ( e : Error ) => catchError ( e , 'Unhandled Rejection' )
65+
66+ process . on ( 'uncaughtException' , uncaughtException )
67+ process . on ( 'unhandledRejection' , unhandledRejection )
68+
69+ dispose . push ( ( ) => {
70+ process . off ( 'uncaughtException' , uncaughtException )
71+ process . off ( 'unhandledRejection' , unhandledRejection )
72+ } )
73+ }
74+
75+ export async function startVitestExecutor ( options : ContextExecutorOptions ) {
76+ // @ts -expect-error injected untyped global
77+ const state = ( ) : WorkerGlobalState => globalThis . __vitest_worker__ || options . state
78+ const rpc = ( ) => state ( ) . rpc
79+
80+ process . exit = ( code = process . exitCode || 0 ) : never => {
81+ throw new Error ( `process.exit unexpectedly called with "${ code } "` )
82+ }
6783
68- process . on ( 'uncaughtException' , e => catchError ( e , 'Uncaught Exception' ) )
69- process . on ( 'unhandledRejection' , e => catchError ( e , 'Unhandled Rejection' ) )
84+ listenForErrors ( state )
7085
7186 const getTransformMode = ( ) => {
7287 return state ( ) . environment . transformMode ?? 'ssr'
0 commit comments