11import type { WorkerGlobalState } from '../../types/worker'
22import { serializeValue } from '@vitest/utils/serialize'
33
4+ // Store globals in case tests overwrite them
5+ const processListeners = process . listeners . bind ( process )
6+ const processOn = process . on . bind ( process )
7+ const processOff = process . off . bind ( process )
8+
49const dispose : ( ( ) => void ) [ ] = [ ]
510
611export function listenForErrors ( state : ( ) => WorkerGlobalState ) : void {
@@ -10,7 +15,7 @@ export function listenForErrors(state: () => WorkerGlobalState): void {
1015 function catchError ( err : any , type : string , event : 'uncaughtException' | 'unhandledRejection' ) {
1116 const worker = state ( )
1217
13- const listeners = process . listeners ( event as 'uncaughtException' )
18+ const listeners = processListeners ( event as 'uncaughtException' )
1419 // if there is another listener, assume that it's handled by user code
1520 // one is Vitest's own listener
1621 if ( listeners . length > 1 ) {
@@ -32,11 +37,11 @@ export function listenForErrors(state: () => WorkerGlobalState): void {
3237 const uncaughtException = ( e : Error ) => catchError ( e , 'Uncaught Exception' , 'uncaughtException' )
3338 const unhandledRejection = ( e : Error ) => catchError ( e , 'Unhandled Rejection' , 'unhandledRejection' )
3439
35- process . on ( 'uncaughtException' , uncaughtException )
36- process . on ( 'unhandledRejection' , unhandledRejection )
40+ processOn ( 'uncaughtException' , uncaughtException )
41+ processOn ( 'unhandledRejection' , unhandledRejection )
3742
3843 dispose . push ( ( ) => {
39- process . off ( 'uncaughtException' , uncaughtException )
40- process . off ( 'unhandledRejection' , unhandledRejection )
44+ processOff ( 'uncaughtException' , uncaughtException )
45+ processOff ( 'unhandledRejection' , unhandledRejection )
4146 } )
4247}
0 commit comments