Skip to content

Commit f45edb4

Browse files
cjihrigtargos
authored andcommittedJul 28, 2024
test_runner: refactor and simplify internals
This commit refactors some of the internals of the test runner. PR-URL: #53921 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent e907236 commit f45edb4

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed
 

‎lib/internal/test_runner/harness.js

+20-22
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ function setup(root) {
208208
};
209209
},
210210
counters: null,
211-
shouldColorizeTestFiles: false,
211+
shouldColorizeTestFiles: shouldColorizeTestFiles(globalOptions.destinations),
212212
teardown: exitHandler,
213213
snapshotManager: null,
214214
};
@@ -218,48 +218,46 @@ function setup(root) {
218218
}
219219

220220
let globalRoot;
221-
let reportersSetup;
222-
function getGlobalRoot() {
221+
let asyncBootstrap;
222+
function lazyBootstrapRoot() {
223223
if (!globalRoot) {
224224
globalRoot = createTestTree({ __proto__: null, entryFile: process.argv?.[1] });
225225
globalRoot.reporter.on('test:fail', (data) => {
226226
if (data.todo === undefined || data.todo === false) {
227227
process.exitCode = kGenericUserError;
228228
}
229229
});
230-
reportersSetup = setupTestReporters(globalRoot.reporter);
231-
globalRoot.harness.shouldColorizeTestFiles ||= shouldColorizeTestFiles(globalRoot);
230+
asyncBootstrap = setupTestReporters(globalRoot.reporter);
232231
}
233232
return globalRoot;
234233
}
235234

236235
async function startSubtest(subtest) {
237-
if (reportersSetup) {
236+
if (asyncBootstrap) {
238237
// Only incur the overhead of awaiting the Promise once.
239-
await reportersSetup;
240-
reportersSetup = undefined;
241-
}
242-
243-
const root = getGlobalRoot();
244-
if (!root.harness.bootstrapComplete) {
245-
root.harness.bootstrapComplete = true;
246-
queueMicrotask(() => {
247-
root.harness.allowTestsToRun = true;
248-
root.processPendingSubtests();
249-
});
238+
await asyncBootstrap;
239+
asyncBootstrap = undefined;
240+
if (!subtest.root.harness.bootstrapComplete) {
241+
subtest.root.harness.bootstrapComplete = true;
242+
queueMicrotask(() => {
243+
subtest.root.harness.allowTestsToRun = true;
244+
subtest.root.processPendingSubtests();
245+
});
246+
}
250247
}
251248

252249
await subtest.start();
253250
}
254251

255252
function runInParentContext(Factory) {
256253
function run(name, options, fn, overrides) {
257-
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
254+
const parent = testResources.get(executionAsyncId()) || lazyBootstrapRoot();
258255
const subtest = parent.createSubtest(Factory, name, options, fn, overrides);
259-
if (!(parent instanceof Suite)) {
260-
return startSubtest(subtest);
256+
if (parent instanceof Suite) {
257+
return PromiseResolve();
261258
}
262-
return PromiseResolve();
259+
260+
return startSubtest(subtest);
263261
}
264262

265263
const test = (name, options, fn) => {
@@ -286,7 +284,7 @@ function runInParentContext(Factory) {
286284

287285
function hook(hook) {
288286
return (fn, options) => {
289-
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
287+
const parent = testResources.get(executionAsyncId()) || lazyBootstrapRoot();
290288
parent.createHook(hook, fn, {
291289
__proto__: null,
292290
...options,

‎lib/internal/test_runner/runner.js

-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ const {
7070
convertStringToRegExp,
7171
countCompletedTest,
7272
kDefaultPattern,
73-
shouldColorizeTestFiles,
7473
} = require('internal/test_runner/utils');
7574
const { Glob } = require('internal/fs/glob');
7675
const { once } = require('events');
@@ -552,7 +551,6 @@ function run(options = kEmptyObject) {
552551
}
553552

554553
const root = createTestTree({ __proto__: null, concurrency, timeout, signal });
555-
root.harness.shouldColorizeTestFiles ||= shouldColorizeTestFiles(root);
556554

557555
if (process.env.NODE_TEST_CONTEXT !== undefined) {
558556
process.emitWarning('node:test run() is being called recursively within a test file. skipping running files.');

‎lib/internal/test_runner/test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ const kHookFailure = 'hookFailed';
7575
const kDefaultTimeout = null;
7676
const noop = FunctionPrototype;
7777
const kShouldAbort = Symbol('kShouldAbort');
78-
const kFilename = process.argv?.[1];
7978
const kHookNames = ObjectSeal(['before', 'after', 'beforeEach', 'afterEach']);
8079
const kUnwrapErrors = new SafeSet()
8180
.add(kTestCodeFailure).add(kHookFailure)
@@ -508,7 +507,7 @@ class Test extends AsyncResource {
508507
this.diagnostic(warning);
509508
}
510509

511-
if (loc === undefined || kFilename === undefined) {
510+
if (loc === undefined) {
512511
this.loc = undefined;
513512
} else {
514513
this.loc = {

‎lib/internal/test_runner/utils.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,9 @@ function tryBuiltinReporter(name) {
131131
return require(builtinPath);
132132
}
133133

134-
function shouldColorizeTestFiles(rootTest) {
134+
function shouldColorizeTestFiles(destinations) {
135135
// This function assumes only built-in destinations (stdout/stderr) supports coloring
136-
const { reporters, destinations } = parseCommandLine();
137-
return ArrayPrototypeSome(reporters, (_, index) => {
136+
return ArrayPrototypeSome(destinations, (_, index) => {
138137
const destination = kBuiltinDestinations.get(destinations[index]);
139138
return destination && shouldColorize(destination);
140139
});

0 commit comments

Comments
 (0)