Skip to content

Commit 1eda87c

Browse files
JacopoPatroclotargos
authored andcommitted
test_runner: add level parameter to reporter.diagnostic
Added a parameter to allow severity-based formatting for diagnostic messages. Defaults to 'info'. This update enables better control over message presentation (e.g., coloring) based on severity levels such as 'info', 'warn', and 'error'. Refs: #55922 PR-URL: #57923 Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 0885546 commit 1eda87c

7 files changed

Lines changed: 59 additions & 4 deletions

File tree

‎doc/api/test.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,11 @@ defined. The corresponding declaration ordered event is `'test:start'`.
30163016
`undefined` if the test was run through the REPL.
30173017
*`message` {string} The diagnostic message.
30183018
*`nesting` {number} The nesting level of the test.
3019+
*`level` {string} The severity level of the diagnostic message.
3020+
Possible values are:
3021+
*`'info'`: Informational messages.
3022+
*`'warn'`: Warnings.
3023+
*`'error'`: Errors.
30193024

30203025
Emitted when [`context.diagnostic`][] is called.
30213026
This event is guaranteed to be emitted in the same order as the tests are

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ class SpecReporter extends Transform {
9494
case'test:stderr':
9595
case'test:stdout':
9696
returndata.message;
97-
case'test:diagnostic':
98-
return`${reporterColorMap[type]}${indent(data.nesting)}${reporterUnicodeSymbolMap[type]}${data.message}${colors.white}\n`;
97+
case'test:diagnostic':{
98+
constdiagnosticColor=reporterColorMap[data.level]||reporterColorMap['test:diagnostic'];
99+
return`${diagnosticColor}${indent(data.nesting)}${reporterUnicodeSymbolMap[type]}${data.message}${colors.white}\n`;
100+
}
99101
case'test:coverage':
100102
returngetCoverageReport(indent(data.nesting),data.summary,
101103
reporterUnicodeSymbolMap['test:coverage'],colors.blue,true);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ const reporterColorMap = {
3737
get'test:diagnostic'(){
3838
returncolors.blue;
3939
},
40+
get'info'(){
41+
returncolors.blue;
42+
},
43+
get'warn'(){
44+
returncolors.yellow;
45+
},
46+
get'error'(){
47+
returncolors.red;
48+
},
4049
};
4150

4251
functionindent(nesting){

‎lib/internal/test_runner/test.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ class Test extends AsyncResource {
12351235
if(actual<threshold){
12361236
harness.success=false;
12371237
process.exitCode=kGenericUserError;
1238-
reporter.diagnostic(nesting,loc,`Error: ${NumberPrototypeToFixed(actual,2)}% ${name} coverage does not meet threshold of ${threshold}%.`);
1238+
reporter.diagnostic(nesting,loc,`Error: ${NumberPrototypeToFixed(actual,2)}% ${name} coverage does not meet threshold of ${threshold}%.`,'error');
12391239
}
12401240
}
12411241

‎lib/internal/test_runner/tests_stream.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,12 @@ class TestsStream extends Readable {
116116
});
117117
}
118118

119-
diagnostic(nesting,loc,message){
119+
diagnostic(nesting,loc,message,level='info'){
120120
this[kEmitMessage]('test:diagnostic',{
121121
__proto__: null,
122122
nesting,
123123
message,
124+
level,
124125
...loc,
125126
});
126127
}

‎test/parallel/test-runner-coverage-thresholds.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@ for (const coverage of coverages) {
9090
assert(!findCoverageFileForPid(result.pid));
9191
});
9292

93+
test(`test failing ${coverage.flag} with red color`,()=>{
94+
constresult=spawnSync(process.execPath,[
95+
'--test',
96+
'--experimental-test-coverage',
97+
'--test-coverage-exclude=!test/**',
98+
`${coverage.flag}=99`,
99+
'--test-reporter','spec',
100+
fixture,
101+
],{
102+
env: { ...process.env,FORCE_COLOR: '3'},
103+
});
104+
105+
conststdout=result.stdout.toString();
106+
// eslint-disable-next-line no-control-regex
107+
constredColorRegex=/\u001b\[31mError:\d{2}\.\d{2}%\w+coveragedoesnotmeetthresholdof99%/;
108+
assert.match(stdout,redColorRegex,'Expected red color code not found in diagnostic message');
109+
assert.strictEqual(result.status,1);
110+
assert(!findCoverageFileForPid(result.pid));
111+
});
112+
93113
test(`test failing ${coverage.flag}`,()=>{
94114
constresult=spawnSync(process.execPath,[
95115
'--test',

‎test/parallel/test-runner-run.mjs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
3333
forawait(const_ofstream);
3434
});
3535

36+
it('should emit diagnostic events with level parameter',async()=>{
37+
constdiagnosticEvents=[];
38+
39+
conststream=run({
40+
files: [join(testFixtures,'coverage.js')],
41+
reporter: 'spec',
42+
});
43+
44+
stream.on('test:diagnostic',(event)=>{
45+
diagnosticEvents.push(event);
46+
});
47+
// eslint-disable-next-line no-unused-vars
48+
forawait(const_ofstream);
49+
assert(diagnosticEvents.length>0,'No diagnostic events were emitted');
50+
constinfoEvent=diagnosticEvents.find((e)=>e.level==='info');
51+
assert(infoEvent,'No diagnostic events with level "info" were emitted');
52+
});
53+
3654
constargPrintingFile=join(testFixtures,'print-arguments.js');
3755
it('should allow custom arguments via execArgv',async()=>{
3856
constresult=awaitrun({files: [argPrintingFile],execArgv: ['-p','"Printed"']}).compose(spec).toArray();

0 commit comments

Comments
 (0)