From 5167a852aca4d42dfe32ce6b832ef58e51d74ac1 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 ++++++++++++++++++++++++++++++ 1 file changed, 35 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'); + } + } +};