Skip to content

Commit 05db979

Browse files
authored
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: #52092 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 97b2c53 commit 05db979

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

lib/internal/test_runner/harness.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const {
2525
setupTestReporters,
2626
shouldColorizeTestFiles,
2727
} = require('internal/test_runner/utils');
28+
const { queueMicrotask } = require('internal/process/task_queues');
2829
const { bigint: hrtime } = process.hrtime;
2930

3031
const testResources = new SafeMap();
@@ -181,6 +182,7 @@ function setup(root) {
181182

182183
root.harness = {
183184
__proto__: null,
185+
allowTestsToRun: false,
184186
bootstrapComplete: false,
185187
coverage: FunctionPrototypeBind(collectCoverage, null, root, coverage),
186188
counters: {
@@ -219,7 +221,16 @@ function getGlobalRoot() {
219221

220222
async function startSubtest(subtest) {
221223
await reportersSetup;
222-
getGlobalRoot().harness.bootstrapComplete = true;
224+
225+
const root = getGlobalRoot();
226+
if (!root.harness.bootstrapComplete) {
227+
root.harness.bootstrapComplete = true;
228+
queueMicrotask(() => {
229+
root.harness.allowTestsToRun = true;
230+
root.processPendingSubtests();
231+
});
232+
}
233+
223234
await subtest.start();
224235
}
225236

lib/internal/test_runner/runner.js

+1
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ function run(options = kEmptyObject) {
538538
}
539539
const runFiles = () => {
540540
root.harness.bootstrapComplete = true;
541+
root.harness.allowTestsToRun = true;
541542
return SafePromiseAllSettledReturnVoid(testFiles, (path) => {
542543
const subtest = runTestFile(path, filesWatcher, opts);
543544
filesWatcher?.runningSubtests.set(path, subtest);

lib/internal/test_runner/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ class Test extends AsyncResource {
599599
// it. Otherwise, return a Promise to the caller and mark the test as
600600
// pending for later execution.
601601
this.reporter.enqueue(this.nesting, this.loc, this.name);
602-
if (!this.parent.hasConcurrency()) {
602+
if (!this.root.harness.allowTestsToRun || !this.parent.hasConcurrency()) {
603603
const deferred = createDeferredPromise();
604604

605605
deferred.test = this;

test/fixtures/test-runner/output/source_mapped_locations.snapshot

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ not ok 1 - fails
2121
*
2222
*
2323
*
24+
*
25+
*
26+
*
2427
...
2528
1..1
2629
# tests 1

0 commit comments

Comments
 (0)