Skip to content

Commit

Permalink
fix suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
pulkit-30 committed Dec 12, 2023
1 parent 9f64e75 commit ca75b72
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const { exitCodes: { kGenericUserError } } = internalBinding('errors');
const { kEmptyObject } = require('internal/util');
const { kCancelledByParent, Test, Suite } = require('internal/test_runner/test');
const {
colorizeTestFiles,
parseCommandLine,
reporterScope,
setupTestReporters,
shouldColorizeTestFiles,
} = require('internal/test_runner/utils');
const { bigint: hrtime } = process.hrtime;

Expand Down Expand Up @@ -207,7 +207,7 @@ function getGlobalRoot() {
}
});
reportersSetup = setupTestReporters(globalRoot.reporter);
colorizeTestFiles(globalRoot);
globalRoot.harness.shouldColorizeTestFiles ||= shouldColorizeTestFiles(globalRoot);
}
return globalRoot;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ const {
} = require('internal/test_runner/test');

const {
colorizeTestFiles,
convertStringToRegExp,
countCompletedTest,
kDefaultPattern,
shouldColorizeTestFiles,
} = require('internal/test_runner/utils');
const { Glob } = require('internal/fs/glob');
const { once } = require('events');
Expand Down Expand Up @@ -488,7 +488,7 @@ function run(options = kEmptyObject) {
}

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

if (process.env.NODE_TEST_CONTEXT !== undefined) {
return root.reporter;
Expand Down
11 changes: 6 additions & 5 deletions lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const {
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypeFlatMap,
ArrayPrototypeForEach,
ArrayPrototypePush,
ArrayPrototypeReduce,
ArrayPrototypeSome,
ObjectGetOwnPropertyDescriptor,
MathFloor,
MathMax,
Expand Down Expand Up @@ -129,11 +129,12 @@ function tryBuiltinReporter(name) {
return require(builtinPath);
}

function colorizeTestFiles(rootTest) {
function shouldColorizeTestFiles(rootTest) {
// This function assumes only built-in destinations (stdout/stderr) supports coloring
const { reporters, destinations } = parseCommandLine();
ArrayPrototypeForEach(reporters, (_, index) => {
return ArrayPrototypeSome(reporters, (_, index) => {
const destination = kBuiltinDestinations.get(destinations[index]);
rootTest.harness.shouldColorizeTestFiles ||= shouldColorize(destination);
return destination && shouldColorize(destination);
});
}

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

module.exports = {
colorizeTestFiles,
convertStringToRegExp,
countCompletedTest,
createDeferredCallback,
Expand All @@ -430,5 +430,6 @@ module.exports = {
parseCommandLine,
reporterScope,
setupTestReporters,
shouldColorizeTestFiles,
getCoverageReport,
};
19 changes: 19 additions & 0 deletions test/parallel/test-runner-reporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,23 @@ describe('node:test reporters', { concurrency: true }, () => {
assert.strictEqual(child.stdout.toString(), 'Going to throw an error\n');
assert.match(child.stderr.toString(), /Emitted 'error' event on Duplex instance/);
});

it('should support stdout as a destination with spec reporter', async () => {
process.env.FORCE_COLOR = '1';
const file = tmpdir.resolve(`${tmpFiles++}.txt`);
const child = spawnSync(process.execPath,
['--test', '--test-reporter', 'spec', '--test-reporter-destination', file, testFile]);
assert.strictEqual(child.stderr.toString(), '');
assert.strictEqual(child.stdout.toString(), '');
const fileConent = fs.readFileSync(file, 'utf8');
assert.match(fileConent, /▶ nested/);
assert.match(fileConent, /✔ ok/);
assert.match(fileConent, /✖ failing/);
assert.match(fileConent, /ℹ tests 4/);
assert.match(fileConent, /ℹ pass 2/);
assert.match(fileConent, /ℹ fail 2/);
assert.match(fileConent, /ℹ cancelled 0/);
assert.match(fileConent, /ℹ skipped 0/);
assert.match(fileConent, /ℹ todo 0/);
});
});

0 comments on commit ca75b72

Please sign in to comment.