Skip to content

Commit d0fb5ce

Browse files
committed
test_runner: pass FORCE_COLOR to child process
1 parent fbb24cf commit d0fb5ce

File tree

7 files changed

+58
-3
lines changed

7 files changed

+58
-3
lines changed

lib/internal/test_runner/harness.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ function setup(root) {
179179
topLevel: 0,
180180
suites: 0,
181181
},
182+
shouldColorizeTestFiles: false,
182183
};
183184
root.startTime = hrtime();
184185
return root;

lib/internal/test_runner/runner.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
326326
stdio.push('ipc');
327327
env.WATCH_REPORT_DEPENDENCIES = '1';
328328
}
329+
if (root.harness.shouldColorizeTestFiles) {
330+
env.FORCE_COLOR = '1';
331+
}
329332

330333
const child = spawn(process.execPath, args, { signal: t.signal, encoding: 'utf8', env, stdio });
331334
runningProcesses.set(path, child);

lib/internal/test_runner/utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const { createWriteStream } = require('fs');
1616
const { pathToFileURL } = require('internal/url');
1717
const { createDeferredPromise } = require('internal/util');
1818
const { getOptionValue } = require('internal/options');
19-
const { green, red, white } = require('internal/util/colors');
19+
const { green, red, white, shouldColorize } = require('internal/util/colors');
2020

2121
const {
2222
codes: {
@@ -115,9 +115,10 @@ function tryBuiltinReporter(name) {
115115
return require(builtinPath);
116116
}
117117

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

122123
// Load the test reporter passed to --test-reporter
123124
let reporter = tryBuiltinReporter(name);
@@ -154,7 +155,7 @@ async function getReportersMap(reporters, destinations) {
154155

155156
async function setupTestReporters(rootTest) {
156157
const { reporters, destinations } = parseCommandLine();
157-
const reportersMap = await getReportersMap(reporters, destinations);
158+
const reportersMap = await getReportersMap(reporters, destinations, rootTest);
158159
for (let i = 0; i < reportersMap.length; i++) {
159160
const { reporter, destination } = reportersMap[i];
160161
compose(rootTest.reporter, reporter).pipe(destination);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const test = require('node:test');
4+
console.log({ foo: 'bar' });
5+
test('passing test', () => {
6+
console.log(1);
7+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
const common = require('../../../common');
3+
const { once } = require('node:events');
4+
const { spawn } = require('node:child_process');
5+
const fixtures = require('../../../common/fixtures');
6+
7+
(async function run() {
8+
const test = fixtures.path('test-runner/output/arbitrary-output-colored-1.js');
9+
await once(spawn(process.execPath, ['--test', test], { stdio: 'inherit' }), 'exit');
10+
await once(spawn(process.execPath, ['--test', '--test-reporter', 'tap', test], { stdio: 'inherit' }), 'exit');
11+
})().then(common.mustCall());
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{ foo: [32m'bar'[39m }
2+
[33m1[39m
3+
[32m✔ passing test [90m(*ms)[39m[39m
4+
[34mℹ tests 1[39m
5+
[34mℹ suites 0[39m
6+
[34mℹ pass 1[39m
7+
[34mℹ fail 0[39m
8+
[34mℹ cancelled 0[39m
9+
[34mℹ skipped 0[39m
10+
[34mℹ todo 0[39m
11+
[34mℹ duration_ms *[39m
12+
TAP version 13
13+
# { foo: [32m'bar'[39m }
14+
# [33m1[39m
15+
# Subtest: passing test
16+
ok 1 - passing test
17+
---
18+
duration_ms: *
19+
...
20+
1..1
21+
# tests 1
22+
# suites 0
23+
# pass 1
24+
# fail 0
25+
# cancelled 0
26+
# skipped 0
27+
# todo 0
28+
# duration_ms *

test/parallel/test-runner-output.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ const tests = [
4646
{ name: 'test-runner/output/unresolved_promise.js' },
4747
{ name: 'test-runner/output/default_output.js', transform: specTransform, tty: true },
4848
{ name: 'test-runner/output/arbitrary-output.js' },
49+
{
50+
name: 'test-runner/output/arbitrary-output-colored.js',
51+
transform: snapshot.transform(specTransform, replaceTestDuration), tty: true
52+
},
4953
{ name: 'test-runner/output/dot_output_custom_columns.js', transform: specTransform, tty: true },
5054
].map(({ name, tty, transform }) => ({
5155
name,

0 commit comments

Comments
 (0)