Skip to content

Commit

Permalink
refresh colors in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Dec 8, 2023
1 parent 30667be commit 1d5f540
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions lib/internal/test_runner/reporter/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,11 @@ const {
const assert = require('assert');
const Transform = require('internal/streams/transform');
const { inspectWithNoCustomRetry } = require('internal/errors');
const { green, blue, red, white, gray, shouldColorize } = require('internal/util/colors');
const colors = require('internal/util/colors');
const { kSubtestsFailed } = require('internal/test_runner/test');
const { getCoverageReport } = require('internal/test_runner/utils');
const { relative } = require('path');

const inspectOptions = { __proto__: null, colors: shouldColorize(process.stdout), breakLength: Infinity };

const colors = {
'__proto__': null,
'test:fail': red,
'test:pass': green,
'test:diagnostic': blue,
};
const symbols = {
'__proto__': null,
'test:fail': '\u2716 ',
Expand All @@ -42,9 +34,19 @@ class SpecReporter extends Transform {
#indentMemo = new SafeMap();
#failedTests = [];
#cwd = process.cwd();
#inspectOptions;
#colors

Check failure on line 38 in lib/internal/test_runner/reporter/spec.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon

constructor() {
super({ __proto__: null, writableObjectMode: true });
colors.refresh();
this.#inspectOptions = { __proto__: null, colors: colors.shouldColorize(process.stdout), breakLength: Infinity };
this.#colors = {
'__proto__': null,
'test:fail': colors.red,
'test:pass': colors.green,
'test:diagnostic': colors.blue,
};
}

#indent(nesting) {
Expand All @@ -62,15 +64,15 @@ class SpecReporter extends Transform {
const message = ArrayPrototypeJoin(
RegExpPrototypeSymbolSplit(
hardenRegExp(/\r?\n/),
inspectWithNoCustomRetry(err, inspectOptions),
inspectWithNoCustomRetry(err, this.#inspectOptions),
), `\n${indent} `);
return `\n${indent} ${message}\n`;
}
#formatTestReport(type, data, prefix = '', indent = '', hasChildren = false) {
let color = colors[type] ?? white;
let color = this.#colors[type] ?? colors.white;
let symbol = symbols[type] ?? ' ';
const { skip, todo } = data;
const duration_ms = data.details?.duration_ms ? ` ${gray}(${data.details.duration_ms}ms)${white}` : '';
const duration_ms = data.details?.duration_ms ? ` ${colors.gray}(${data.details.duration_ms}ms)${colors.white}` : '';
let title = `${data.name}${duration_ms}`;

if (skip !== undefined) {
Expand All @@ -82,13 +84,13 @@ class SpecReporter extends Transform {
if (hasChildren) {
// If this test has had children - it was already reported, so slightly modify the output
const err = data.details?.error?.failureType === 'subtestsFailed' ? '' : error;
return `${prefix}${indent}${color}${symbols['arrow:right']}${white}${title}\n${err}`;
return `${prefix}${indent}${color}${symbols['arrow:right']}${colors.white}${title}\n${err}`;
}
if (skip !== undefined) {
color = gray;
color = colors.gray;
symbol = symbols['hyphen:minus'];
}
return `${prefix}${indent}${color}${symbol}${title}${white}${error}`;
return `${prefix}${indent}${color}${symbol}${title}${colors.white}${error}`;
}
#handleTestReportEvent(type, data) {
const subtest = ArrayPrototypeShift(this.#stack); // This is the matching `test:start` event
Expand Down Expand Up @@ -130,9 +132,9 @@ class SpecReporter extends Transform {
case 'test:stdout':
return data.message;
case 'test:diagnostic':
return `${colors[type]}${this.#indent(data.nesting)}${symbols[type]}${data.message}${white}\n`;
return `${this.#colors[type]}${this.#indent(data.nesting)}${symbols[type]}${data.message}${colors.white}\n`;
case 'test:coverage':
return getCoverageReport(this.#indent(data.nesting), data.summary, symbols['test:coverage'], blue, true);
return getCoverageReport(this.#indent(data.nesting), data.summary, symbols['test:coverage'], colors.blue, true);
}
}
_transform({ type, data }, encoding, callback) {
Expand All @@ -143,7 +145,7 @@ class SpecReporter extends Transform {
callback(null, '');
return;
}
const results = [`\n${colors['test:fail']}${symbols['test:fail']}failing tests:${white}\n`];
const results = [`\n${this.#colors['test:fail']}${symbols['test:fail']}failing tests:${colors.white}\n`];
for (let i = 0; i < this.#failedTests.length; i++) {
const test = this.#failedTests[i];
const relPath = relative(this.#cwd, test.file);
Expand Down

0 comments on commit 1d5f540

Please sign in to comment.