@@ -42,14 +42,11 @@ export interface ContextExecutorOptions {
42
42
43
43
const bareVitestRegexp = / ^ @ ? v i t e s t ( \/ | $ ) /
44
44
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 ) [ ] = [ ]
49
46
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
53
50
54
51
function catchError ( err : unknown , type : string ) {
55
52
const worker = state ( )
@@ -60,13 +57,31 @@ export async function startVitestExecutor(options: ContextExecutorOptions) {
60
57
error . VITEST_TEST_PATH = relative ( state ( ) . config . root , worker . filepath )
61
58
error . VITEST_AFTER_ENV_TEARDOWN = worker . environmentTeardownRun
62
59
}
63
- rpc ( ) . onUnhandledError ( error , type )
60
+ state ( ) . rpc . onUnhandledError ( error , type )
64
61
}
65
62
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
+ }
67
83
68
- process . on ( 'uncaughtException' , e => catchError ( e , 'Uncaught Exception' ) )
69
- process . on ( 'unhandledRejection' , e => catchError ( e , 'Unhandled Rejection' ) )
84
+ listenForErrors ( state )
70
85
71
86
const getTransformMode = ( ) => {
72
87
return state ( ) . environment . transformMode ?? 'ssr'
0 commit comments