Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: fixed test object is incorrectly passed to setup() #50982

Merged
merged 5 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const { exitCodes: { kGenericUserError } } = internalBinding('errors');
const { kEmptyObject } = require('internal/util');
const { kCancelledByParent, Test, Suite } = require('internal/test_runner/test');
const {
colorizeTestFiles,
parseCommandLine,
reporterScope,
setupTestReporters,
Expand Down Expand Up @@ -205,7 +206,8 @@ function getGlobalRoot() {
process.exitCode = kGenericUserError;
}
});
reportersSetup = setupTestReporters(globalRoot);
reportersSetup = setupTestReporters(globalRoot.reporter);
colorizeTestFiles(globalRoot);
pulkit-30 marked this conversation as resolved.
Show resolved Hide resolved
}
return globalRoot;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const {
} = require('internal/test_runner/test');

const {
colorizeTestFiles,
convertStringToRegExp,
countCompletedTest,
kDefaultPattern,
Expand Down Expand Up @@ -487,6 +488,8 @@ function run(options = kEmptyObject) {
}

const root = createTestTree({ __proto__: null, concurrency, timeout, signal });
colorizeTestFiles(root);
pulkit-30 marked this conversation as resolved.
Show resolved Hide resolved

if (process.env.NODE_TEST_CONTEXT !== undefined) {
return root.reporter;
}
Expand All @@ -512,7 +515,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]);
rootTest.harness.shouldColorizeTestFiles ||= shouldColorize(destination);
});
}
pulkit-30 marked this conversation as resolved.
Show resolved Hide resolved

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);
pulkit-30 marked this conversation as resolved.
Show resolved Hide resolved

// 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
13 changes: 13 additions & 0 deletions test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,19 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
code: 'ERR_INVALID_ARG_TYPE'
}));
});

it('should pass instance of stream to setup', async () => {
rluvaton marked this conversation as resolved.
Show resolved Hide resolved
const stream = run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')],
setup: common.mustCall((root) => {
assert.strictEqual(root.constructor.name, '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