From e2ae4367f4eac39f0fcf7eaf99dabba67bea59ed Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Sun, 17 Mar 2024 00:17:36 -0400 Subject: [PATCH] test_runner: run top level tests in a microtask This commit updates the test harness to prevent top level tests from executing immediately. This allows certain config data, such as filtering options, to be discovered before running the tests. PR-URL: https://github.com/nodejs/node/pull/52092 Reviewed-By: Moshe Atlow Reviewed-By: Chemi Atlow --- lib/internal/test_runner/harness.js | 13 ++++++++++++- lib/internal/test_runner/runner.js | 1 + lib/internal/test_runner/test.js | 2 +- .../output/source_mapped_locations.snapshot | 3 +++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/internal/test_runner/harness.js b/lib/internal/test_runner/harness.js index 0db5ba4a60f..0e48d8f2a36 100644 --- a/lib/internal/test_runner/harness.js +++ b/lib/internal/test_runner/harness.js @@ -26,6 +26,7 @@ const { setupTestReporters, shouldColorizeTestFiles, } = require('internal/test_runner/utils'); +const { queueMicrotask } = require('internal/process/task_queues'); const { bigint: hrtime } = process.hrtime; const testResources = new SafeMap(); @@ -189,6 +190,7 @@ function setup(root) { root.harness = { __proto__: null, + allowTestsToRun: false, bootstrapComplete: false, watching: false, coverage: FunctionPrototypeBind(collectCoverage, null, root, coverage), @@ -232,7 +234,16 @@ function getGlobalRoot() { async function startSubtest(subtest) { await reportersSetup; - getGlobalRoot().harness.bootstrapComplete = true; + + const root = getGlobalRoot(); + if (!root.harness.bootstrapComplete) { + root.harness.bootstrapComplete = true; + queueMicrotask(() => { + root.harness.allowTestsToRun = true; + root.processPendingSubtests(); + }); + } + await subtest.start(); } diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 9e6bf4efb51..463729d67d1 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -590,6 +590,7 @@ function run(options) { } const runFiles = () => { root.harness.bootstrapComplete = true; + root.harness.allowTestsToRun = true; return SafePromiseAllSettledReturnVoid(testFiles, (path) => { const subtest = runTestFile(path, filesWatcher, opts); filesWatcher?.runningSubtests.set(path, subtest); diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index ceb68a0ce23..9ce486a3054 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -584,7 +584,7 @@ class Test extends AsyncResource { // it. Otherwise, return a Promise to the caller and mark the test as // pending for later execution. this.reporter.enqueue(this.nesting, this.loc, this.name); - if (!this.parent.hasConcurrency()) { + if (!this.root.harness.allowTestsToRun || !this.parent.hasConcurrency()) { const deferred = createDeferredPromise(); deferred.test = this; diff --git a/test/fixtures/test-runner/output/source_mapped_locations.snapshot b/test/fixtures/test-runner/output/source_mapped_locations.snapshot index 24c3ee8d113..29b70fd0d08 100644 --- a/test/fixtures/test-runner/output/source_mapped_locations.snapshot +++ b/test/fixtures/test-runner/output/source_mapped_locations.snapshot @@ -21,6 +21,9 @@ not ok 1 - fails * * * + * + * + * ... 1..1 # tests 1