8
8
SafeMap,
9
9
SafePromiseAllReturnVoid,
10
10
} = primordials ;
11
+
11
12
const { getCallerLocation } = internalBinding ( 'util' ) ;
12
13
const {
13
14
createHook,
@@ -230,6 +231,9 @@ function setupProcessState(root, globalOptions) {
230
231
const rejectionHandler =
231
232
createProcessEventHandler ( 'unhandledRejection' , root ) ;
232
233
const coverage = configureCoverage ( root , globalOptions ) ;
234
+
235
+ const disposableStack = new DisposableStack ( ) ; // eslint-disable-line no-undef
236
+
233
237
const exitHandler = async ( kill ) => {
234
238
if ( root . subtests . length === 0 && ( root . hooks . before . length > 0 || root . hooks . after . length > 0 ) ) {
235
239
// Run global before/after hooks in case there are no tests
@@ -254,27 +258,21 @@ function setupProcessState(root, globalOptions) {
254
258
}
255
259
256
260
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 ( ) ;
264
262
} ;
265
263
266
264
const terminationHandler = async ( ) => {
267
265
await exitHandler ( true ) ;
268
266
process . exit ( ) ;
269
267
} ;
270
268
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 ) ) ;
274
272
// TODO(MoLow): Make it configurable to hook when isTestRunner === false.
275
273
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 ) ) ;
278
276
}
279
277
280
278
root . harness . coverage = FunctionPrototypeBind ( collectCoverage , null , root , coverage ) ;
0 commit comments