From 367f9e77f37545b2a027573cf532aad5181e6b45 Mon Sep 17 00:00:00 2001 From: Eddie Abbondanzio Date: Tue, 23 Jul 2024 02:49:52 -0400 Subject: [PATCH] test_runner: cleanup global event listeners after run PR-URL: https://github.com/nodejs/node/pull/53878 Fixes: https://github.com/nodejs/node/issues/53868 Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- lib/internal/test_runner/harness.js | 7 ++++++- lib/internal/test_runner/runner.js | 5 ++++- test/parallel/test-runner-run.mjs | 10 ++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/internal/test_runner/harness.js b/lib/internal/test_runner/harness.js index 3a00e5a3fcceab..c7d951d6b485a5 100644 --- a/lib/internal/test_runner/harness.js +++ b/lib/internal/test_runner/harness.js @@ -170,8 +170,13 @@ function setup(root) { kCancelledByParent)); hook.disable(); - process.removeListener('unhandledRejection', rejectionHandler); process.removeListener('uncaughtException', exceptionHandler); + process.removeListener('unhandledRejection', rejectionHandler); + process.removeListener('beforeExit', exitHandler); + if (globalOptions.isTestRunner) { + process.removeListener('SIGINT', terminationHandler); + process.removeListener('SIGTERM', terminationHandler); + } }; const terminationHandler = () => { diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 0fd1a84a60f2b9..c761e3616f964d 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -591,6 +591,7 @@ function run(options) { } let postRun = () => root.postRun(); + let teardown = () => root.harness.teardown(); let filesWatcher; const opts = { __proto__: null, @@ -604,6 +605,7 @@ function run(options) { if (watch) { filesWatcher = watchFiles(testFiles, opts); postRun = undefined; + teardown = undefined; } const runFiles = () => { root.harness.bootstrapComplete = true; @@ -615,7 +617,8 @@ function run(options) { }); }; - PromisePrototypeThen(PromisePrototypeThen(PromiseResolve(setup?.(root.reporter)), runFiles), postRun); + const setupPromise = PromiseResolve(setup?.(root.reporter)); + PromisePrototypeThen(PromisePrototypeThen(PromisePrototypeThen(setupPromise, runFiles), postRun), teardown); return root.reporter; } diff --git a/test/parallel/test-runner-run.mjs b/test/parallel/test-runner-run.mjs index 6aefc07fc73d6f..548ea78ce02227 100644 --- a/test/parallel/test-runner-run.mjs +++ b/test/parallel/test-runner-run.mjs @@ -542,3 +542,13 @@ describe('forceExit', () => { }); }); }); + + +// exitHandler doesn't run until after the tests / after hooks finish. +process.on('exit', () => { + assert.strictEqual(process.listeners('uncaughtException').length, 0); + assert.strictEqual(process.listeners('unhandledRejection').length, 0); + assert.strictEqual(process.listeners('beforeExit').length, 0); + assert.strictEqual(process.listeners('SIGINT').length, 0); + assert.strictEqual(process.listeners('SIGTERM').length, 0); +});