Skip to content

Commit 9f88f40

Browse files
MoLowrichardlau
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 6c24155 commit 9f88f40

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

‎lib/internal/test_runner/reporter/spec.js‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,11 @@ const{
1414
constassert=require('assert');
1515
constTransform=require('internal/streams/transform');
1616
const{ inspectWithNoCustomRetry }=require('internal/errors');
17-
const{ green, blue, red, white, gray, shouldColorize }=require('internal/util/colors');
17+
constcolors=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-
constinspectOptions={__proto__: null,colors: shouldColorize(process.stdout),breakLength: Infinity};
23-
24-
constcolors={
25-
'__proto__': null,
26-
'test:fail': red,
27-
'test:pass': green,
28-
'test:diagnostic': blue,
29-
};
3022
constsymbols={
3123
'__proto__': null,
3224
'test:fail': '\u2716 ',
@@ -42,9 +34,19 @@ class SpecReporter extends Transform{
4234
#indentMemo =newSafeMap();
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
constmessage=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-
letcolor=colors[type]??white;
72+
letcolor=this.#colors[type]??colors.white;
7173
letsymbol=symbols[type]??' ';
7274
const{ skip, todo }=data;
73-
constduration_ms=data.details?.duration_ms ? ` ${gray}(${data.details.duration_ms}ms)${white}` : '';
75+
constduration_ms=data.details?.duration_ms ? ` ${colors.gray}(${data.details.duration_ms}ms)${colors.white}` : '';
7476
lettitle=`${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
consterr=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
constsubtest=ArrayPrototypeShift(this.#stack);// This is the matching `test:start` event
@@ -130,9 +132,9 @@ class SpecReporter extends Transform{
130132
case'test:stdout':
131133
returndata.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-
returngetCoverageReport(this.#indent(data.nesting),data.summary,symbols['test:coverage'],blue,true);
137+
returngetCoverageReport(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-
constresults=[`\n${colors['test:fail']}${symbols['test:fail']}failing tests:${white}\n`];
148+
constresults=[`\n${this.#colors['test:fail']}${symbols['test:fail']}failing tests:${colors.white}\n`];
147149
for(leti=0;i<this.#failedTests.length;i++){
148150
consttest=this.#failedTests[i];
149151
constrelPath=relative(this.#cwd,test.file);

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

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

77
(asyncfunctionrun(){
88
consttest=fixtures.path('test-runner/output/arbitrary-output-colored-1.js');
9-
awaitonce(spawn(process.execPath,['--test',test],{stdio: 'inherit',env: {FORCE_COLOR: 1}}),'exit');
10-
awaitonce(spawn(process.execPath,['--test','--test-reporter','tap',test],{stdio: 'inherit',env: {FORCE_COLOR: 1}}),'exit');
9+
constreset=fixtures.path('test-runner/output/reset-color-depth.js');
10+
awaitonce(spawn(process.execPath,['-r',reset,'--test',test],{stdio: 'inherit'}),'exit');
11+
awaitonce(spawn(process.execPath,['-r',reset,'--test','--test-reporter','tap',test],{stdio: 'inherit'}),'exit');
1112
})().then(common.mustCall());
Lines changed: 5 additions & 0 deletions
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)