GeezerVison is showing github.com — test_runner: add level parameter to reporter.diagnostic · nodejs/node@1eda87c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -3016,6 +3016,11 @@ defined. The corresponding declaration ordered event is `'test:start'`. | 3016 | 3016 | `undefined` if the test was run through the REPL. | 3017 | 3017 | * `message` {string} The diagnostic message. | 3018 | 3018 | * `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. | 3019 | 3024 |
| 3020 | 3025 | Emitted when [`context.diagnostic`][] is called. | 3021 | 3026 | This event is guaranteed to be emitted in the same order as the tests are | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/reporter/spec.js +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -94,8 +94,10 @@ class SpecReporter extends Transform { | 94 | 94 | case 'test:stderr': | 95 | 95 | case 'test:stdout': | 96 | 96 | 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 | + } | 99 | 101 | case 'test:coverage': | 100 | 102 | return getCoverageReport(indent(data.nesting), data.summary, | 101 | 103 | reporterUnicodeSymbolMap['test:coverage'], colors.blue, true); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/reporter/utils.js +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,6 +37,15 @@ const reporterColorMap = { | 37 | 37 | get 'test:diagnostic'() { | 38 | 38 | return colors.blue; | 39 | 39 | }, | | 40 | + get 'info'() { | | 41 | + return colors.blue; | | 42 | + }, | | 43 | + get 'warn'() { | | 44 | + return colors.yellow; | | 45 | + }, | | 46 | + get 'error'() { | | 47 | + return colors.red; | | 48 | + }, | 40 | 49 | }; | 41 | 50 |
| 42 | 51 | function indent(nesting) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/test.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1235,7 +1235,7 @@ class Test extends AsyncResource { | 1235 | 1235 | if (actual < threshold) { | 1236 | 1236 | harness.success = false; | 1237 | 1237 | 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'); | 1239 | 1239 | } | 1240 | 1240 | } | 1241 | 1241 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/tests_stream.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -116,11 +116,12 @@ class TestsStream extends Readable { | 116 | 116 | }); | 117 | 117 | } | 118 | 118 |
| 119 | | - diagnostic(nesting, loc, message) { | | 119 | + diagnostic(nesting, loc, message, level = 'info') { | 120 | 120 | this[kEmitMessage]('test:diagnostic', { | 121 | 121 | __proto__: null, | 122 | 122 | nesting, | 123 | 123 | message, | | 124 | + level, | 124 | 125 | ...loc, | 125 | 126 | }); | 126 | 127 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-runner-coverage-thresholds.js +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -90,6 +90,26 @@ for (const coverage of coverages) { | 90 | 90 | assert(!findCoverageFileForPid(result.pid)); | 91 | 91 | }); | 92 | 92 |
| | 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 | + | 93 | 113 | test(`test failing ${coverage.flag}`, () => { | 94 | 114 | const result = spawnSync(process.execPath, [ | 95 | 115 | '--test', | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-runner-run.mjs +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,6 +33,24 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { | 33 | 33 | for await (const _ of stream); | 34 | 34 | }); | 35 | 35 |
| | 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 | + | 36 | 54 | const argPrintingFile = join(testFixtures, 'print-arguments.js'); | 37 | 55 | it('should allow custom arguments via execArgv', async () => { | 38 | 56 | const result = await run({ files: [argPrintingFile], execArgv: ['-p', '"Printed"'] }).compose(spec).toArray(); | |
|