From ecaeaf8f873911c5feeeac3ce32508a7bdb4b5d3 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 8 Aug 2024 17:06:12 -0400 Subject: [PATCH] test_runner: pass harness object as option to root test This commit initializes the root harness object before the root test and passes the harness as an option to the root test constructor. This commit also attaches the global configuration to the harness. This will allow the parseCommandLine() call in test.js to be removed, as those values are now available via the root test. PR-URL: https://github.com/nodejs/node/pull/54353 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Matteo Collina --- lib/internal/test_runner/harness.js | 76 +++++++++++++++-------------- lib/internal/test_runner/test.js | 3 +- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/lib/internal/test_runner/harness.js b/lib/internal/test_runner/harness.js index ac52307cc38be5..de0cf82682af31 100644 --- a/lib/internal/test_runner/harness.js +++ b/lib/internal/test_runner/harness.js @@ -35,7 +35,42 @@ let globalRoot; testResources.set(reporterScope.asyncId(), reporterScope); function createTestTree(options = kEmptyObject) { - globalRoot = setup(new Test({ __proto__: null, ...options, name: '' })); + const globalOptions = parseCommandLine(); + const harness = { + __proto__: null, + allowTestsToRun: false, + bootstrapPromise: resolvedPromise, + watching: false, + config: globalOptions, + coverage: null, + resetCounters() { + harness.counters = { + __proto__: null, + all: 0, + failed: 0, + passed: 0, + cancelled: 0, + skipped: 0, + todo: 0, + topLevel: 0, + suites: 0, + }; + }, + counters: null, + shouldColorizeTestFiles: shouldColorizeTestFiles(globalOptions.destinations), + teardown: null, + snapshotManager: null, + }; + + harness.resetCounters(); + globalRoot = new Test({ + __proto__: null, + ...options, + harness, + name: '', + }); + setupProcessState(globalRoot, globalOptions, harness); + globalRoot.startTime = hrtime(); return globalRoot; } @@ -127,15 +162,7 @@ function collectCoverage(rootTest, coverage) { return summary; } -function setup(root) { - if (root.startTime !== null) { - return root; - } - - // Parse the command line options before the hook is enabled. We don't want - // global input validation errors to end up in the uncaughtException handler. - const globalOptions = parseCommandLine(); - +function setupProcessState(root, globalOptions) { const hook = createHook({ __proto__: null, init(asyncId, type, triggerAsyncId, resource) { @@ -195,33 +222,8 @@ function setup(root) { process.on('SIGTERM', terminationHandler); } - root.harness = { - __proto__: null, - allowTestsToRun: false, - bootstrapPromise: resolvedPromise, - watching: false, - coverage: FunctionPrototypeBind(collectCoverage, null, root, coverage), - resetCounters() { - root.harness.counters = { - __proto__: null, - all: 0, - failed: 0, - passed: 0, - cancelled: 0, - skipped: 0, - todo: 0, - topLevel: 0, - suites: 0, - }; - }, - counters: null, - shouldColorizeTestFiles: shouldColorizeTestFiles(globalOptions.destinations), - teardown: exitHandler, - snapshotManager: null, - }; - root.harness.resetCounters(); - root.startTime = hrtime(); - return root; + root.harness.coverage = FunctionPrototypeBind(collectCoverage, null, root, coverage); + root.harness.teardown = exitHandler; } function lazyBootstrapRoot() { diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index ab41a93fc081f9..9fe5cf9ccd0e9c 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -395,6 +395,7 @@ class Test extends AsyncResource { this.timeout = kDefaultTimeout; this.entryFile = entryFile; this.root = this; + this.harness = options.harness; this.hooks = { __proto__: null, before: [], @@ -416,6 +417,7 @@ class Test extends AsyncResource { this.timeout = parent.timeout; this.entryFile = parent.entryFile; this.root = parent.root; + this.harness = null; this.hooks = { __proto__: null, before: [], @@ -480,7 +482,6 @@ class Test extends AsyncResource { ); this.fn = fn; - this.harness = null; // Configured on the root test by the test harness. this.mock = null; this.plan = null; this.expectedAssertions = plan;