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

File tree

7 files changed

+59
-4
lines changed

7 files changed

+59
-4
lines changed

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
return data.message;
97-
case 'test:diagnostic':
98-
return `${reporterColorMap[type]}${indent(data.nesting)}${reporterUnicodeSymbolMap[type]}${data.message}${colors.white}\n`;
97+
case 'test:diagnostic':{
98+
const diagnosticColor = 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
return getCoverageReport(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
return colors.blue;
3939
},
40+
get 'info'() {
41+
return colors.blue;
42+
},
43+
get 'warn'() {
44+
return colors.yellow;
45+
},
46+
get 'error'() {
47+
return colors.red;
48+
},
4049
};
4150

4251
function indent(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+
const result = 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+
const stdout = result.stdout.toString();
106+
// eslint-disable-next-line no-control-regex
107+
const redColorRegex = /\u001b\[31m Error: \d{2}\.\d{2}% \w+ coverage does not meet threshold of 99%/;
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
const result = 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
for await (const _ of stream);
3434
});
3535

36+
it('should emit diagnostic events with level parameter', async () => {
37+
const diagnosticEvents = [];
38+
39+
const stream = 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+
for await (const _ of stream);
49+
assert(diagnosticEvents.length > 0, 'No diagnostic events were emitted');
50+
const infoEvent = diagnosticEvents.find((e) => e.level === 'info');
51+
assert(infoEvent, 'No diagnostic events with level "info" were emitted');
52+
});
53+
3654
const argPrintingFile = join(testFixtures, 'print-arguments.js');
3755
it('should allow custom arguments via execArgv', async () => {
3856
const result = await run({ files: [argPrintingFile], execArgv: ['-p', '"Printed"'] }).compose(spec).toArray();

0 commit comments

Comments
 (0)