-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add error only reporter for node:test
This commit introduces a node:test reporter to the common utils. This reporter can be used to silence output other than errors from node:test. This is useful because in Node's own test suite, the output of node:test is included in the output of the Python test runner. Refs: #49120 PR-URL: #56438 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Pietro Marchini <[email protected]>
- Loading branch information
Showing
12 changed files
with
93 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
const { relative } = require('node:path'); | ||
const { inspect } = require('node:util'); | ||
const cwd = process.cwd(); | ||
|
||
module.exports = async function* errorReporter(source) { | ||
for await (const event of source) { | ||
if (event.type === 'test:fail') { | ||
const { name, details, line, column, file } = event.data; | ||
let { error } = details; | ||
|
||
if (error?.failureType === 'subtestsFailed') { | ||
// In the interest of keeping things concise, skip failures that are | ||
// only due to nested failures. | ||
continue; | ||
} | ||
|
||
if (error?.code === 'ERR_TEST_FAILURE') { | ||
error = error.cause; | ||
} | ||
|
||
const output = [ | ||
`Test failure: '${name}'`, | ||
]; | ||
|
||
if (file) { | ||
output.push(`Location: ${relative(cwd, file)}:${line}:${column}`); | ||
} | ||
|
||
output.push(inspect(error)); | ||
output.push('\n'); | ||
yield output.join('\n'); | ||
|
||
if (process.env.FAIL_FAST) { | ||
yield `\nBailing on failed test: ${event.data.name}\n`; | ||
process.exitCode = 1; | ||
process.emit('SIGINT'); | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const assert = require('node:assert'); | ||
const { test } = require('node:test'); | ||
|
||
test('fail', () => { | ||
assert.fail('a.mjs fail'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const assert = require('node:assert'); | ||
const { test } = require('node:test'); | ||
|
||
test('fail', () => { | ||
assert.fail('b.mjs fail'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
const assert = require('node:assert'); | ||
const { spawnSync } = require('node:child_process'); | ||
const { test } = require('node:test'); | ||
const cwd = fixtures.path('test-runner', 'error-reporter-fail-fast'); | ||
|
||
test('all tests failures reported without FAIL_FAST flag', async () => { | ||
const args = [ | ||
'--test-reporter=./test/common/test-error-reporter.js', | ||
'--test-concurrency=1', | ||
'--test', | ||
`${cwd}/*.mjs`, | ||
]; | ||
const cp = spawnSync(process.execPath, args); | ||
const failureCount = (cp.stdout.toString().match(/Test failure:/g) || []).length; | ||
assert.strictEqual(failureCount, 2); | ||
}); | ||
|
||
test('FAIL_FAST stops test execution after first failure', async () => { | ||
const args = [ | ||
'--test-reporter=./test/common/test-error-reporter.js', | ||
'--test-concurrency=1', | ||
'--test', | ||
`${cwd}/*.mjs`, | ||
]; | ||
const cp = spawnSync(process.execPath, args, { env: { ...process.env, FAIL_FAST: 'true' } }); | ||
const failureCount = (cp.stdout.toString().match(/Test failure:/g) || []).length; | ||
assert.strictEqual(failureCount, 1); | ||
}); |