Skip to content

Commit

Permalink
test_runner: fixed test object is incorrectly passed to setup()
Browse files Browse the repository at this point in the history
  • Loading branch information
pulkit-30 committed Dec 11, 2023
1 parent 6e90fed commit 6a3ebf7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {
parseCommandLine,
reporterScope,
setupTestReporters,
colorizeTestFiles,
} = require('internal/test_runner/utils');
const { bigint: hrtime } = process.hrtime;

Expand Down Expand Up @@ -205,7 +206,8 @@ function getGlobalRoot() {
process.exitCode = kGenericUserError;
}
});
reportersSetup = setupTestReporters(globalRoot);
reportersSetup = setupTestReporters(globalRoot.reporter);
colorizeTestFiles(globalRoot);
}
return globalRoot;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const {
convertStringToRegExp,
countCompletedTest,
kDefaultPattern,
colorizeTestFiles,
} = require('internal/test_runner/utils');
const { Glob } = require('internal/fs/glob');
const { once } = require('events');
Expand Down Expand Up @@ -491,6 +492,7 @@ function run(options = kEmptyObject) {
return root.reporter;
}
let testFiles = files ?? createTestFileList();
colorizeTestFiles(root);

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

PromisePrototypeThen(PromisePrototypeThen(PromiseResolve(setup?.(root)), runFiles), postRun);
PromisePrototypeThen(PromisePrototypeThen(PromiseResolve(setup?.(root.reporter)), runFiles), postRun);

return root.reporter;
}
Expand Down
19 changes: 14 additions & 5 deletions lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypeFlatMap,
ArrayPrototypeForEach,
ArrayPrototypePush,
ArrayPrototypeReduce,
ObjectGetOwnPropertyDescriptor,
Expand Down Expand Up @@ -128,10 +129,17 @@ function tryBuiltinReporter(name) {
return require(builtinPath);
}

async function getReportersMap(reporters, destinations, rootTest) {
function colorizeTestFiles(rootTest) {
const { reporters, destinations } = parseCommandLine();
ArrayPrototypeForEach(reporters, (_, index) => {
const destination = kBuiltinDestinations.get(destinations[index]) ?? createWriteStream(destinations[index]);
rootTest.harness.shouldColorizeTestFiles ||= shouldColorize(destination);
});
}

async function getReportersMap(reporters, destinations) {
return SafePromiseAllReturnArrayLike(reporters, async (name, i) => {
const destination = kBuiltinDestinations.get(destinations[i]) ?? createWriteStream(destinations[i]);
rootTest.harness.shouldColorizeTestFiles ||= shouldColorize(destination);

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

const reporterScope = new AsyncResource('TestReporterScope');
const setupTestReporters = reporterScope.bind(async (rootTest) => {
const setupTestReporters = reporterScope.bind(async (rootReporter) => {
const { reporters, destinations } = parseCommandLine();
const reportersMap = await getReportersMap(reporters, destinations, rootTest);
const reportersMap = await getReportersMap(reporters, destinations);
for (let i = 0; i < reportersMap.length; i++) {
const { reporter, destination } = reportersMap[i];
compose(rootTest.reporter, reporter).pipe(destination);
compose(rootReporter, reporter).pipe(destination);
}
});

Expand Down Expand Up @@ -413,6 +421,7 @@ function getCoverageReport(pad, summary, symbol, color, table) {
}

module.exports = {
colorizeTestFiles,
convertStringToRegExp,
countCompletedTest,
createDeferredCallback,
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Flags: --expose-internals

import * as common from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import { join } from 'node:path';
import { describe, it, run } from 'node:test';
import { dot, spec, tap } from 'node:test/reporters';
import assert from 'node:assert';
import stream from 'internal/test_runner/tests_stream';

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

describe('require(\'node:test\').run', { concurrency: true }, () => {
Expand Down Expand Up @@ -465,6 +469,19 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
code: 'ERR_INVALID_ARG_TYPE'
}));
});

it('should pass instance of stream to setup', async () => {
const stream = run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')],
setup: common.mustCall((root) => {
assert(root instanceof TestsStream);
}),
});
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
});

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

0 comments on commit 6a3ebf7

Please sign in to comment.