From b191f09e6ee7d40798454e8545d83e53cab0e63c Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sun, 5 Mar 2023 12:38:32 -0500 Subject: [PATCH] test_runner: give the root test a harness reference This commit replaces the 'coverage' reference inside of the Test class with a more generic harness reference which includes coverage. This will let the root test more easily track process level state such as code coverage, uncaughtException handlers, and the state of bootstrapping. PR-URL: https://github.com/nodejs/node/pull/46962 Reviewed-By: Moshe Atlow Reviewed-By: Yagiz Nizipli --- lib/internal/test_runner/harness.js | 7 ++++++- lib/internal/test_runner/test.js | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/internal/test_runner/harness.js b/lib/internal/test_runner/harness.js index a5cec210df2f73..42cc5613f2a2c8 100644 --- a/lib/internal/test_runner/harness.js +++ b/lib/internal/test_runner/harness.js @@ -139,7 +139,7 @@ function setup(root) { createProcessEventHandler('unhandledRejection', root); const coverage = configureCoverage(root, globalOptions); const exitHandler = () => { - root.coverage = collectCoverage(root, coverage); + root.harness.coverage = collectCoverage(root, coverage); root.postRun(new ERR_TEST_FAILURE( 'Promise resolution is still pending but the event loop has already resolved', kCancelledByParent)); @@ -163,6 +163,11 @@ function setup(root) { process.on('SIGTERM', terminationHandler); } + root.harness = { + __proto__: null, + bootstrapComplete: false, + coverage: null, + }; root.startTime = hrtime(); wasRootSetup.add(root); diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index b165808686e0ad..ffbf2d257aed62 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -252,7 +252,7 @@ class Test extends AsyncResource { this.#outerSignal?.addEventListener('abort', this.#abortHandler); this.fn = fn; - this.coverage = null; // Configured on the root test by the test harness. + this.harness = null; // Configured on the root test by the test harness. this.mock = null; this.name = name; this.parent = parent; @@ -651,8 +651,8 @@ class Test extends AsyncResource { this.reporter.diagnostic(this.nesting, kFilename, `todo ${counters.todo}`); this.reporter.diagnostic(this.nesting, kFilename, `duration_ms ${this.#duration()}`); - if (this.coverage) { - this.reporter.coverage(this.nesting, kFilename, this.coverage); + if (this.harness?.coverage) { + this.reporter.coverage(this.nesting, kFilename, this.harness.coverage); } this.reporter.push(null);