Skip to content

Commit 048349e

Browse files
MoLowRafaelGSS
authored andcommitted
test: fix test runner colored output test
PR-URL: #51064 Reviewed-By: Raz Luvaton <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 637ffce commit 048349e

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

lib/internal/test_runner/reporter/spec.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,11 @@ const {
1414
const assert = require('assert');
1515
const Transform = require('internal/streams/transform');
1616
const { inspectWithNoCustomRetry } = require('internal/errors');
17-
const { green, blue, red, white, gray, shouldColorize } = require('internal/util/colors');
17+
const colors = require('internal/util/colors');
1818
const { kSubtestsFailed } = require('internal/test_runner/test');
1919
const { getCoverageReport } = require('internal/test_runner/utils');
2020
const { relative } = require('path');
2121

22-
const inspectOptions = { __proto__: null, colors: shouldColorize(process.stdout), breakLength: Infinity };
23-
24-
const colors = {
25-
'__proto__': null,
26-
'test:fail': red,
27-
'test:pass': green,
28-
'test:diagnostic': blue,
29-
};
3022
const symbols = {
3123
'__proto__': null,
3224
'test:fail': '\u2716 ',
@@ -42,9 +34,19 @@ class SpecReporter extends Transform {
4234
#indentMemo = new SafeMap();
4335
#failedTests = [];
4436
#cwd = process.cwd();
37+
#inspectOptions;
38+
#colors;
4539

4640
constructor() {
4741
super({ __proto__: null, writableObjectMode: true });
42+
colors.refresh();
43+
this.#inspectOptions = { __proto__: null, colors: colors.shouldColorize(process.stdout), breakLength: Infinity };
44+
this.#colors = {
45+
'__proto__': null,
46+
'test:fail': colors.red,
47+
'test:pass': colors.green,
48+
'test:diagnostic': colors.blue,
49+
};
4850
}
4951

5052
#indent(nesting) {
@@ -62,15 +64,15 @@ class SpecReporter extends Transform {
6264
const message = ArrayPrototypeJoin(
6365
RegExpPrototypeSymbolSplit(
6466
hardenRegExp(/\r?\n/),
65-
inspectWithNoCustomRetry(err, inspectOptions),
67+
inspectWithNoCustomRetry(err, this.#inspectOptions),
6668
), `\n${indent} `);
6769
return `\n${indent} ${message}\n`;
6870
}
6971
#formatTestReport(type, data, prefix = '', indent = '', hasChildren = false) {
70-
let color = colors[type] ?? white;
72+
let color = this.#colors[type] ?? colors.white;
7173
let symbol = symbols[type] ?? ' ';
7274
const { skip, todo } = data;
73-
const duration_ms = data.details?.duration_ms ? ` ${gray}(${data.details.duration_ms}ms)${white}` : '';
75+
const duration_ms = data.details?.duration_ms ? ` ${colors.gray}(${data.details.duration_ms}ms)${colors.white}` : '';
7476
let title = `${data.name}${duration_ms}`;
7577

7678
if (skip !== undefined) {
@@ -82,13 +84,13 @@ class SpecReporter extends Transform {
8284
if (hasChildren) {
8385
// If this test has had children - it was already reported, so slightly modify the output
8486
const err = data.details?.error?.failureType === 'subtestsFailed' ? '' : error;
85-
return `${prefix}${indent}${color}${symbols['arrow:right']}${white}${title}\n${err}`;
87+
return `${prefix}${indent}${color}${symbols['arrow:right']}${colors.white}${title}\n${err}`;
8688
}
8789
if (skip !== undefined) {
88-
color = gray;
90+
color = colors.gray;
8991
symbol = symbols['hyphen:minus'];
9092
}
91-
return `${prefix}${indent}${color}${symbol}${title}${white}${error}`;
93+
return `${prefix}${indent}${color}${symbol}${title}${colors.white}${error}`;
9294
}
9395
#handleTestReportEvent(type, data) {
9496
const subtest = ArrayPrototypeShift(this.#stack); // This is the matching `test:start` event
@@ -130,9 +132,9 @@ class SpecReporter extends Transform {
130132
case 'test:stdout':
131133
return data.message;
132134
case 'test:diagnostic':
133-
return `${colors[type]}${this.#indent(data.nesting)}${symbols[type]}${data.message}${white}\n`;
135+
return `${this.#colors[type]}${this.#indent(data.nesting)}${symbols[type]}${data.message}${colors.white}\n`;
134136
case 'test:coverage':
135-
return getCoverageReport(this.#indent(data.nesting), data.summary, symbols['test:coverage'], blue, true);
137+
return getCoverageReport(this.#indent(data.nesting), data.summary, symbols['test:coverage'], colors.blue, true);
136138
}
137139
}
138140
_transform({ type, data }, encoding, callback) {
@@ -143,7 +145,7 @@ class SpecReporter extends Transform {
143145
callback(null, '');
144146
return;
145147
}
146-
const results = [`\n${colors['test:fail']}${symbols['test:fail']}failing tests:${white}\n`];
148+
const results = [`\n${this.#colors['test:fail']}${symbols['test:fail']}failing tests:${colors.white}\n`];
147149
for (let i = 0; i < this.#failedTests.length; i++) {
148150
const test = this.#failedTests[i];
149151
const relPath = relative(this.#cwd, test.file);

test/fixtures/test-runner/output/arbitrary-output-colored.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const fixtures = require('../../../common/fixtures');
66

77
(async function run() {
88
const test = fixtures.path('test-runner/output/arbitrary-output-colored-1.js');
9-
await once(spawn(process.execPath, ['--test', test], { stdio: 'inherit', env: { FORCE_COLOR: 1 } }), 'exit');
10-
await once(spawn(process.execPath, ['--test', '--test-reporter', 'tap', test], { stdio: 'inherit', env: { FORCE_COLOR: 1 } }), 'exit');
9+
const reset = fixtures.path('test-runner/output/reset-color-depth.js');
10+
await once(spawn(process.execPath, ['-r', reset, '--test', test], { stdio: 'inherit' }), 'exit');
11+
await once(spawn(process.execPath, ['-r', reset, '--test', '--test-reporter', 'tap', test], { stdio: 'inherit' }), 'exit');
1112
})().then(common.mustCall());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
// Make tests OS-independent by overriding stdio getColorDepth().
4+
process.stdout.getColorDepth = () => 8;
5+
process.stderr.getColorDepth = () => 8;

0 commit comments

Comments
 (0)