GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test_runner: add level parameter to reporter.diagnostic · nodejs/node@1eda87c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • test.md
  • lib/internal/test_runner
    • reporter
      • spec.js
      • utils.js
    • test.js
    • tests_stream.js
  • test/parallel
    • test-runner-coverage-thresholds.js
    • test-runner-run.mjs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/test.md‎

Copy file name to clipboardExpand all lines: doc/api/test.md
+5Lines changed: 5 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/test_runner/reporter/spec.js
+4-2Lines 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);
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/test_runner/reporter/utils.js
+9Lines 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) {
Collapse file

‎lib/internal/test_runner/test.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/test.js
+1-1Lines 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

Collapse file

‎lib/internal/test_runner/tests_stream.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/tests_stream.js
+2-1Lines 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
}
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-runner-coverage-thresholds.js
+20Lines 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',
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-runner-run.mjs
+18Lines 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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.