Skip to content

Commit

Permalink
test_runner: pass FORCE_COLOR to child process
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed May 23, 2023
1 parent fbb24cf commit d0fb5ce
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ function setup(root) {
topLevel: 0,
suites: 0,
},
shouldColorizeTestFiles: false,
};
root.startTime = hrtime();
return root;
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
stdio.push('ipc');
env.WATCH_REPORT_DEPENDENCIES = '1';
}
if (root.harness.shouldColorizeTestFiles) {
env.FORCE_COLOR = '1';
}

const child = spawn(process.execPath, args, { signal: t.signal, encoding: 'utf8', env, stdio });
runningProcesses.set(path, child);
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const { createWriteStream } = require('fs');
const { pathToFileURL } = require('internal/url');
const { createDeferredPromise } = require('internal/util');
const { getOptionValue } = require('internal/options');
const { green, red, white } = require('internal/util/colors');
const { green, red, white, shouldColorize } = require('internal/util/colors');

const {
codes: {
Expand Down Expand Up @@ -115,9 +115,10 @@ function tryBuiltinReporter(name) {
return require(builtinPath);
}

async function getReportersMap(reporters, destinations) {
async function getReportersMap(reporters, destinations, rootTest) {
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 @@ -154,7 +155,7 @@ async function getReportersMap(reporters, destinations) {

async function setupTestReporters(rootTest) {
const { reporters, destinations } = parseCommandLine();
const reportersMap = await getReportersMap(reporters, destinations);
const reportersMap = await getReportersMap(reporters, destinations, rootTest);
for (let i = 0; i < reportersMap.length; i++) {
const { reporter, destination } = reportersMap[i];
compose(rootTest.reporter, reporter).pipe(destination);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const test = require('node:test');
console.log({ foo: 'bar' });
test('passing test', () => {
console.log(1);
});
11 changes: 11 additions & 0 deletions test/fixtures/test-runner/output/arbitrary-output-colored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
const common = require('../../../common');
const { once } = require('node:events');
const { spawn } = require('node:child_process');
const fixtures = require('../../../common/fixtures');

(async function run() {
const test = fixtures.path('test-runner/output/arbitrary-output-colored-1.js');
await once(spawn(process.execPath, ['--test', test], { stdio: 'inherit' }), 'exit');
await once(spawn(process.execPath, ['--test', '--test-reporter', 'tap', test], { stdio: 'inherit' }), 'exit');
})().then(common.mustCall());
28 changes: 28 additions & 0 deletions test/fixtures/test-runner/output/arbitrary-output-colored.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{ foo: [32m'bar'[39m }
[33m1[39m
[32m✔ passing test [90m(*ms)[39m[39m
[34mℹ tests 1[39m
[34mℹ suites 0[39m
[34mℹ pass 1[39m
[34mℹ fail 0[39m
[34mℹ cancelled 0[39m
[34mℹ skipped 0[39m
[34mℹ todo 0[39m
[34mℹ duration_ms *[39m
TAP version 13
# { foo: [32m'bar'[39m }
# [33m1[39m
# Subtest: passing test
ok 1 - passing test
---
duration_ms: *
...
1..1
# tests 1
# suites 0
# pass 1
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms *
4 changes: 4 additions & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ const tests = [
{ name: 'test-runner/output/unresolved_promise.js' },
{ name: 'test-runner/output/default_output.js', transform: specTransform, tty: true },
{ name: 'test-runner/output/arbitrary-output.js' },
{
name: 'test-runner/output/arbitrary-output-colored.js',
transform: snapshot.transform(specTransform, replaceTestDuration), tty: true
},
{ name: 'test-runner/output/dot_output_custom_columns.js', transform: specTransform, tty: true },
].map(({ name, tty, transform }) => ({
name,
Expand Down

0 comments on commit d0fb5ce

Please sign in to comment.