Skip to content

Commit c4848c5

Browse files
EddieAbbondanzioRafaelGSS
authored andcommitted
test_runner: cleanup global event listeners after run
PR-URL: #53878 Fixes: #53868 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 7e8a609 commit c4848c5

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

lib/internal/test_runner/harness.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,13 @@ function setup(root) {
170170
kCancelledByParent));
171171

172172
hook.disable();
173-
process.removeListener('unhandledRejection', rejectionHandler);
174173
process.removeListener('uncaughtException', exceptionHandler);
174+
process.removeListener('unhandledRejection', rejectionHandler);
175+
process.removeListener('beforeExit', exitHandler);
176+
if (globalOptions.isTestRunner) {
177+
process.removeListener('SIGINT', terminationHandler);
178+
process.removeListener('SIGTERM', terminationHandler);
179+
}
175180
};
176181

177182
const terminationHandler = () => {

lib/internal/test_runner/runner.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ function run(options = kEmptyObject) {
564564
}
565565

566566
let postRun = () => root.postRun();
567+
let teardown = () => root.harness.teardown();
567568
let filesWatcher;
568569
const opts = {
569570
__proto__: null,
@@ -578,6 +579,7 @@ function run(options = kEmptyObject) {
578579
if (watch) {
579580
filesWatcher = watchFiles(testFiles, opts);
580581
postRun = undefined;
582+
teardown = undefined;
581583
}
582584
const runFiles = () => {
583585
root.harness.bootstrapComplete = true;
@@ -589,7 +591,8 @@ function run(options = kEmptyObject) {
589591
});
590592
};
591593

592-
PromisePrototypeThen(PromisePrototypeThen(PromiseResolve(setup?.(root.reporter)), runFiles), postRun);
594+
const setupPromise = PromiseResolve(setup?.(root.reporter));
595+
PromisePrototypeThen(PromisePrototypeThen(PromisePrototypeThen(setupPromise, runFiles), postRun), teardown);
593596

594597
return root.reporter;
595598
}

test/parallel/test-runner-run.mjs

+10
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,13 @@ describe('forceExit', () => {
551551
});
552552
});
553553
});
554+
555+
556+
// exitHandler doesn't run until after the tests / after hooks finish.
557+
process.on('exit', () => {
558+
assert.strictEqual(process.listeners('uncaughtException').length, 0);
559+
assert.strictEqual(process.listeners('unhandledRejection').length, 0);
560+
assert.strictEqual(process.listeners('beforeExit').length, 0);
561+
assert.strictEqual(process.listeners('SIGINT').length, 0);
562+
assert.strictEqual(process.listeners('SIGTERM').length, 0);
563+
});

0 commit comments

Comments
 (0)