@@ -14,19 +14,11 @@ const {
1414const assert = require('assert');
1515const Transform = require('internal/streams/transform');
1616const { inspectWithNoCustomRetry } = require('internal/errors');
17- const { green, blue, red, white, gray, shouldColorize } = require('internal/util/colors');
17+ const colors = require('internal/util/colors');
1818const { kSubtestsFailed } = require('internal/test_runner/test');
1919const { getCoverageReport } = require('internal/test_runner/utils');
2020const { 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- };
3022const 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);
0 commit comments