@@ -54,8 +54,19 @@ function listenForErrors(state: () => WorkerGlobalState) {
54
54
dispose . forEach ( fn => fn ( ) )
55
55
dispose . length = 0
56
56
57
- function catchError ( err : unknown , type : string ) {
57
+ function catchError ( err : unknown , type : string , event : 'uncaughtException' | 'unhandledRejection' ) {
58
58
const worker = state ( )
59
+
60
+ // if error happens during a test
61
+ if ( worker . current ) {
62
+ const listeners = process . listeners ( event as 'uncaughtException' )
63
+ // if there is another listener, assume that it's handled by user code
64
+ // one is Vitest's own listener
65
+ if ( listeners . length > 1 ) {
66
+ return
67
+ }
68
+ }
69
+
59
70
const error = processError ( err )
60
71
if ( ! isPrimitive ( error ) ) {
61
72
error . VITEST_TEST_NAME = worker . current ?. name
@@ -67,8 +78,8 @@ function listenForErrors(state: () => WorkerGlobalState) {
67
78
state ( ) . rpc . onUnhandledError ( error , type )
68
79
}
69
80
70
- const uncaughtException = ( e : Error ) => catchError ( e , 'Uncaught Exception' )
71
- const unhandledRejection = ( e : Error ) => catchError ( e , 'Unhandled Rejection' )
81
+ const uncaughtException = ( e : Error ) => catchError ( e , 'Uncaught Exception' , 'uncaughtException' )
82
+ const unhandledRejection = ( e : Error ) => catchError ( e , 'Unhandled Rejection' , 'unhandledRejection' )
72
83
73
84
process . on ( 'uncaughtException' , uncaughtException )
74
85
process . on ( 'unhandledRejection' , unhandledRejection )
0 commit comments