Skip to content

Commit f45edb4

Browse files
cjihrigtargos
authored andcommitted
test_runner: refactor and simplify internals
This commit refactors some of the internals of the test runner. PR-URL: #53921 Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]>
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‎

Lines changed: 20 additions & 22 deletions
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
letglobalRoot;
221-
letreportersSetup;
222-
functiongetGlobalRoot(){
221+
letasyncBootstrap;
222+
functionlazyBootstrapRoot(){
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
returnglobalRoot;
234233
}
235234

236235
asyncfunctionstartSubtest(subtest){
237-
if(reportersSetup){
236+
if(asyncBootstrap){
238237
// Only incur the overhead of awaiting the Promise once.
239-
awaitreportersSetup;
240-
reportersSetup=undefined;
241-
}
242-
243-
constroot=getGlobalRoot();
244-
if(!root.harness.bootstrapComplete){
245-
root.harness.bootstrapComplete=true;
246-
queueMicrotask(()=>{
247-
root.harness.allowTestsToRun=true;
248-
root.processPendingSubtests();
249-
});
238+
awaitasyncBootstrap;
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
awaitsubtest.start();
253250
}
254251

255252
functionrunInParentContext(Factory){
256253
functionrun(name,options,fn,overrides){
257-
constparent=testResources.get(executionAsyncId())||getGlobalRoot();
254+
constparent=testResources.get(executionAsyncId())||lazyBootstrapRoot();
258255
constsubtest=parent.createSubtest(Factory,name,options,fn,overrides);
259-
if(!(parentinstanceofSuite)){
260-
returnstartSubtest(subtest);
256+
if(parentinstanceofSuite){
257+
returnPromiseResolve();
261258
}
262-
returnPromiseResolve();
259+
260+
returnstartSubtest(subtest);
263261
}
264262

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

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

‎lib/internal/test_runner/runner.js‎

Lines changed: 0 additions & 2 deletions
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
constroot=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‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ const kHookFailure = 'hookFailed'
7575
constkDefaultTimeout=null;
7676
constnoop=FunctionPrototype;
7777
constkShouldAbort=Symbol('kShouldAbort');
78-
constkFilename=process.argv?.[1];
7978
constkHookNames=ObjectSeal(['before','after','beforeEach','afterEach']);
8079
constkUnwrapErrors=newSafeSet()
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‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,9 @@ function tryBuiltinReporter(name){
131131
returnrequire(builtinPath);
132132
}
133133

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

0 commit comments

Comments
(0)