@@ -438,13 +438,11 @@ describe('error-reporting', () => {
438438 let oldLogger : ( text : string ) => void ;
439439 let logOutput = '' ;
440440 before ( async ( ) => {
441- // This test assumes that only the error-reporting library will be
442- // adding listeners to the 'unhandledRejection' event. Thus we need to
443- // make sure that no listeners for that event exist. If this check
444- // fails, then the reinitialize() method below will need to updated to
445- // more carefully reinitialize the error-reporting library without
446- // interfering with existing listeners of the 'unhandledRejection' event.
447- assert . strictEqual ( process . listenerCount ( 'unhandledRejection' ) , 0 ) ;
441+ // This test assumes that the error reporting library will be adding listeners
442+ // to the 'unhandledRejection' event. Thus we need to make sure other default
443+ // listeners do not interfere. If this check fails, then update the reinitialize
444+ // method below to more carefully reinitialize the error-reporting library.
445+ assert . strictEqual ( process . listenerCount ( 'unhandledRejection' ) , 1 ) ;
448446 oldLogger = console . error ;
449447 // eslint-disable-next-line @typescript-eslint/no-explicit-any
450448 console . error = function ( this , ...args : any [ ] ) {
@@ -456,7 +454,13 @@ describe('error-reporting', () => {
456454 } ) ;
457455
458456 function reinitialize ( extraConfig ?: { } ) {
459- process . removeAllListeners ( 'unhandledRejection' ) ;
457+ for ( const listener of process . listeners ( 'unhandledRejection' ) ) {
458+ // Do not interfere with existing Mocha listener
459+ if ( ! listener . toString ( ) . includes ( 'isMochaError' ) ) {
460+ process . removeListener ( 'unhandledRejection' , listener ) ;
461+ }
462+ }
463+
460464 const initConfiguration = Object . assign (
461465 {
462466 reportMode : 'always' as 'always' ,
0 commit comments