88 SafeMap,
99 SafePromiseAllReturnVoid,
1010} = primordials ;
11+
1112const { getCallerLocation } = internalBinding ( 'util' ) ;
1213const {
1314 createHook,
@@ -230,6 +231,9 @@ function setupProcessState(root, globalOptions) {
230231 const rejectionHandler =
231232 createProcessEventHandler ( 'unhandledRejection' , root ) ;
232233 const coverage = configureCoverage ( root , globalOptions ) ;
234+
235+ const disposableStack = new DisposableStack ( ) ; // eslint-disable-line no-undef
236+
233237 const exitHandler = async ( kill ) => {
234238 if ( root . subtests . length === 0 && ( root . hooks . before . length > 0 || root . hooks . after . length > 0 ) ) {
235239 // Run global before/after hooks in case there are no tests
@@ -254,27 +258,21 @@ function setupProcessState(root, globalOptions) {
254258 }
255259
256260 hook . disable ( ) ;
257- process . removeListener ( 'uncaughtException' , exceptionHandler ) ;
258- process . removeListener ( 'unhandledRejection' , rejectionHandler ) ;
259- process . removeListener ( 'beforeExit' , exitHandler ) ;
260- if ( globalOptions . isTestRunner ) {
261- process . removeListener ( 'SIGINT' , terminationHandler ) ;
262- process . removeListener ( 'SIGTERM' , terminationHandler ) ;
263- }
261+ disposableStack . dispose ( ) ;
264262 } ;
265263
266264 const terminationHandler = async ( ) => {
267265 await exitHandler ( true ) ;
268266 process . exit ( ) ;
269267 } ;
270268
271- process . on ( 'uncaughtException' , exceptionHandler ) ;
272- process . on ( 'unhandledRejection' , rejectionHandler ) ;
273- process . on ( 'beforeExit' , exitHandler ) ;
269+ disposableStack . use ( process . use ( 'uncaughtException' , exceptionHandler ) ) ;
270+ disposableStack . use ( process . use ( 'unhandledRejection' , rejectionHandler ) ) ;
271+ disposableStack . use ( process . use ( 'beforeExit' , exitHandler ) ) ;
274272 // TODO(MoLow): Make it configurable to hook when isTestRunner === false.
275273 if ( globalOptions . isTestRunner ) {
276- process . on ( 'SIGINT' , terminationHandler ) ;
277- process . on ( 'SIGTERM' , terminationHandler ) ;
274+ disposableStack . use ( process . use ( 'SIGINT' , terminationHandler ) ) ;
275+ disposableStack . use ( process . use ( 'SIGTERM' , terminationHandler ) ) ;
278276 }
279277
280278 root . harness . coverage = FunctionPrototypeBind ( collectCoverage , null , root , coverage ) ;
0 commit comments