Skip to content

Commit 6a3ebf7

Browse files
committed
test_runner: fixed test object is incorrectly passed to setup()
1 parent 6e90fed commit 6a3ebf7

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

lib/internal/test_runner/harness.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const {
2323
parseCommandLine,
2424
reporterScope,
2525
setupTestReporters,
26+
colorizeTestFiles,
2627
} = require('internal/test_runner/utils');
2728
const { bigint: hrtime } = process.hrtime;
2829

@@ -205,7 +206,8 @@ function getGlobalRoot() {
205206
process.exitCode = kGenericUserError;
206207
}
207208
});
208-
reportersSetup = setupTestReporters(globalRoot);
209+
reportersSetup = setupTestReporters(globalRoot.reporter);
210+
colorizeTestFiles(globalRoot);
209211
}
210212
return globalRoot;
211213
}

lib/internal/test_runner/runner.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const {
7070
convertStringToRegExp,
7171
countCompletedTest,
7272
kDefaultPattern,
73+
colorizeTestFiles,
7374
} = require('internal/test_runner/utils');
7475
const { Glob } = require('internal/fs/glob');
7576
const { once } = require('events');
@@ -491,6 +492,7 @@ function run(options = kEmptyObject) {
491492
return root.reporter;
492493
}
493494
let testFiles = files ?? createTestFileList();
495+
colorizeTestFiles(root);
494496

495497
if (shard) {
496498
testFiles = ArrayPrototypeFilter(testFiles, (_, index) => index % shard.total === shard.index - 1);
@@ -512,7 +514,7 @@ function run(options = kEmptyObject) {
512514
});
513515
};
514516

515-
PromisePrototypeThen(PromisePrototypeThen(PromiseResolve(setup?.(root)), runFiles), postRun);
517+
PromisePrototypeThen(PromisePrototypeThen(PromiseResolve(setup?.(root.reporter)), runFiles), postRun);
516518

517519
return root.reporter;
518520
}

lib/internal/test_runner/utils.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {
33
ArrayPrototypeJoin,
44
ArrayPrototypeMap,
55
ArrayPrototypeFlatMap,
6+
ArrayPrototypeForEach,
67
ArrayPrototypePush,
78
ArrayPrototypeReduce,
89
ObjectGetOwnPropertyDescriptor,
@@ -128,10 +129,17 @@ function tryBuiltinReporter(name) {
128129
return require(builtinPath);
129130
}
130131

131-
async function getReportersMap(reporters, destinations, rootTest) {
132+
function colorizeTestFiles(rootTest) {
133+
const { reporters, destinations } = parseCommandLine();
134+
ArrayPrototypeForEach(reporters, (_, index) => {
135+
const destination = kBuiltinDestinations.get(destinations[index]) ?? createWriteStream(destinations[index]);
136+
rootTest.harness.shouldColorizeTestFiles ||= shouldColorize(destination);
137+
});
138+
}
139+
140+
async function getReportersMap(reporters, destinations) {
132141
return SafePromiseAllReturnArrayLike(reporters, async (name, i) => {
133142
const destination = kBuiltinDestinations.get(destinations[i]) ?? createWriteStream(destinations[i]);
134-
rootTest.harness.shouldColorizeTestFiles ||= shouldColorize(destination);
135143

136144
// Load the test reporter passed to --test-reporter
137145
let reporter = tryBuiltinReporter(name);
@@ -166,12 +174,12 @@ async function getReportersMap(reporters, destinations, rootTest) {
166174
}
167175

168176
const reporterScope = new AsyncResource('TestReporterScope');
169-
const setupTestReporters = reporterScope.bind(async (rootTest) => {
177+
const setupTestReporters = reporterScope.bind(async (rootReporter) => {
170178
const { reporters, destinations } = parseCommandLine();
171-
const reportersMap = await getReportersMap(reporters, destinations, rootTest);
179+
const reportersMap = await getReportersMap(reporters, destinations);
172180
for (let i = 0; i < reportersMap.length; i++) {
173181
const { reporter, destination } = reportersMap[i];
174-
compose(rootTest.reporter, reporter).pipe(destination);
182+
compose(rootReporter, reporter).pipe(destination);
175183
}
176184
});
177185

@@ -413,6 +421,7 @@ function getCoverageReport(pad, summary, symbol, color, table) {
413421
}
414422

415423
module.exports = {
424+
colorizeTestFiles,
416425
convertStringToRegExp,
417426
countCompletedTest,
418427
createDeferredCallback,

test/parallel/test-runner-run.mjs

+17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
// Flags: --expose-internals
2+
13
import * as common from '../common/index.mjs';
24
import * as fixtures from '../common/fixtures.mjs';
35
import { join } from 'node:path';
46
import { describe, it, run } from 'node:test';
57
import { dot, spec, tap } from 'node:test/reporters';
68
import assert from 'node:assert';
9+
import stream from 'internal/test_runner/tests_stream';
710

11+
const { TestsStream } = stream;
812
const testFixtures = fixtures.path('test-runner');
913

1014
describe('require(\'node:test\').run', { concurrency: true }, () => {
@@ -465,6 +469,19 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
465469
code: 'ERR_INVALID_ARG_TYPE'
466470
}));
467471
});
472+
473+
it('should pass instance of stream to setup', async () => {
474+
const stream = run({
475+
files: [join(testFixtures, 'default-behavior/test/random.cjs')],
476+
setup: common.mustCall((root) => {
477+
assert(root instanceof TestsStream);
478+
}),
479+
});
480+
stream.on('test:fail', common.mustNotCall());
481+
stream.on('test:pass', common.mustCall());
482+
// eslint-disable-next-line no-unused-vars
483+
for await (const _ of stream);
484+
});
468485
});
469486

470487
it('should run with no files', async () => {

0 commit comments

Comments
 (0)