From d2b18acc11fd85bc7be8a12356924e066692009f Mon Sep 17 00:00:00 2001 From: cjihrig Date: Fri, 22 Mar 2024 16:37:11 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/49120 --- test/common/test-error-reporter.js | 35 ++++++++++++++++++++ test/parallel/test-runner-cli-concurrency.js | 1 + 2 files changed, 36 insertions(+) create mode 100644 test/common/test-error-reporter.js diff --git a/test/common/test-error-reporter.js b/test/common/test-error-reporter.js new file mode 100644 index 00000000000000..3098b10885f19d --- /dev/null +++ b/test/common/test-error-reporter.js @@ -0,0 +1,35 @@ +'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'); + } + } +}; diff --git a/test/parallel/test-runner-cli-concurrency.js b/test/parallel/test-runner-cli-concurrency.js index fbabaf08e27279..47efdd0e2797f0 100644 --- a/test/parallel/test-runner-cli-concurrency.js +++ b/test/parallel/test-runner-cli-concurrency.js @@ -1,3 +1,4 @@ +// Flags: --test-reporter=./test/common/test-error-reporter.js 'use strict'; require('../common'); const fixtures = require('../common/fixtures');