Skip to content

Commit 323f96f

Browse files
Ceres6aduh95
authored andcommitted
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]>
1 parent 4e4b0c6 commit 323f96f

12 files changed

+93
-8
lines changed

.github/workflows/build-tarball.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ jobs:
105105
- name: Test
106106
run: |
107107
cd $TAR_DIR
108-
make run-ci -j4 V=1 TEST_CI_ARGS="-p dots --node-args='--test-reporter=spec' --measure-flakiness 9"
108+
make run-ci -j4 V=1 TEST_CI_ARGS="-p dots --node-args='--test-reporter=./test/common/test-error-reporter.js' --measure-flakiness 9"

.github/workflows/coverage-linux-without-intl.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
# TODO(bcoe): fix the couple tests that fail with the inspector enabled.
6969
# The cause is most likely coverage's use of the inspector.
7070
- name: Test
71-
run: NODE_V8_COVERAGE=coverage/tmp make test-cov -j4 V=1 TEST_CI_ARGS="-p dots --node-args='--test-reporter=spec' --measure-flakiness 9" || exit 0
71+
run: NODE_V8_COVERAGE=coverage/tmp make test-cov -j4 V=1 TEST_CI_ARGS="-p dots --node-args='--test-reporter=./test/common/test-error-reporter.js' --measure-flakiness 9" || exit 0
7272
- name: Report JS
7373
run: npx c8 report --check-coverage
7474
env:

.github/workflows/coverage-linux.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
# TODO(bcoe): fix the couple tests that fail with the inspector enabled.
6969
# The cause is most likely coverage's use of the inspector.
7070
- name: Test
71-
run: NODE_V8_COVERAGE=coverage/tmp make test-cov -j4 V=1 TEST_CI_ARGS="-p dots --node-args='--test-reporter=spec' --measure-flakiness 9" || exit 0
71+
run: NODE_V8_COVERAGE=coverage/tmp make test-cov -j4 V=1 TEST_CI_ARGS="-p dots --node-args='--test-reporter=./test/common/test-error-reporter.js' --measure-flakiness 9" || exit 0
7272
- name: Report JS
7373
run: npx c8 report --check-coverage
7474
env:

.github/workflows/doc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ jobs:
4040
name: docs
4141
path: out/doc
4242
- name: Test
43-
run: NODE=$(command -v node) make test-doc-ci TEST_CI_ARGS="-p actions --node-args='--test-reporter=spec' --node-args='--test-reporter-destination=stdout' --measure-flakiness 9"
43+
run: NODE=$(command -v node) make test-doc-ci TEST_CI_ARGS="-p actions --node-args='--test-reporter=./test/common/test-error-reporter.js' --node-args='--test-reporter-destination=stdout' --measure-flakiness 9"

.github/workflows/test-asan.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ jobs:
6363
- name: Build
6464
run: make build-ci -j4 V=1
6565
- name: Test
66-
run: make run-ci -j4 V=1 TEST_CI_ARGS="-p actions --node-args='--test-reporter=spec' --node-args='--test-reporter-destination=stdout' -t 300 --measure-flakiness 9"
66+
run: make run-ci -j4 V=1 TEST_CI_ARGS="-p actions --node-args='--test-reporter=./test/common/test-error-reporter.js' --node-args='--test-reporter-destination=stdout' -t 300 --measure-flakiness 9"

.github/workflows/test-linux.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
- name: Build
5555
run: make -C node build-ci -j4 V=1 CONFIG_FLAGS="--error-on-warn"
5656
- name: Test
57-
run: make -C node run-ci -j4 V=1 TEST_CI_ARGS="-p actions --node-args='--test-reporter=spec' --node-args='--test-reporter-destination=stdout' --measure-flakiness 9"
57+
run: make -C node run-ci -j4 V=1 TEST_CI_ARGS="-p actions --node-args='--test-reporter=./test/common/test-error-reporter.js' --node-args='--test-reporter-destination=stdout' --measure-flakiness 9"
5858
- name: Re-run test in a folder whose name contains unusual chars
5959
run: |
6060
mv node "$DIR"

.github/workflows/test-macos.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ jobs:
8989
- name: Free Space After Build
9090
run: df -h
9191
- name: Test
92-
run: make -C node run-ci -j$(getconf _NPROCESSORS_ONLN) V=1 TEST_CI_ARGS="-p actions --node-args='--test-reporter=spec' --node-args='--test-reporter-destination=stdout' --measure-flakiness 9"
92+
run: make -C node run-ci -j$(getconf _NPROCESSORS_ONLN) V=1 TEST_CI_ARGS="-p actions --node-args='--test-reporter=./test/common/test-error-reporter.js' --node-args='--test-reporter-destination=stdout' --measure-flakiness 9"
9393
- name: Re-run test in a folder whose name contains unusual chars
9494
run: |
9595
mv node "$DIR"

.github/workflows/test-ubsan.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@ jobs:
6464
- name: Build
6565
run: make build-ci -j2 V=1
6666
- name: Test
67-
run: make run-ci -j2 V=1 TEST_CI_ARGS="-p actions --node-args='--test-reporter=spec' --node-args='--test-reporter-destination=stdout' -t 300 --measure-flakiness 9"
67+
run: make run-ci -j2 V=1 TEST_CI_ARGS="-p actions --node-args='--test-reporter=./test/common/test-error-reporter.js' --node-args='--test-reporter-destination=stdout' -t 300 --measure-flakiness 9"

test/common/test-error-reporter.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
const { relative } = require('node:path');
3+
const { inspect } = require('node:util');
4+
const cwd = process.cwd();
5+
6+
module.exports = async function* errorReporter(source) {
7+
for await (const event of source) {
8+
if (event.type === 'test:fail') {
9+
const { name, details, line, column, file } = event.data;
10+
let { error } = details;
11+
12+
if (error?.failureType === 'subtestsFailed') {
13+
// In the interest of keeping things concise, skip failures that are
14+
// only due to nested failures.
15+
continue;
16+
}
17+
18+
if (error?.code === 'ERR_TEST_FAILURE') {
19+
error = error.cause;
20+
}
21+
22+
const output = [
23+
`Test failure: '${name}'`,
24+
];
25+
26+
if (file) {
27+
output.push(`Location: ${relative(cwd, file)}:${line}:${column}`);
28+
}
29+
30+
output.push(inspect(error));
31+
output.push('\n');
32+
yield output.join('\n');
33+
34+
if (process.env.FAIL_FAST) {
35+
yield `\nBailing on failed test: ${event.data.name}\n`;
36+
process.exitCode = 1;
37+
process.emit('SIGINT');
38+
}
39+
}
40+
}
41+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const assert = require('node:assert');
2+
const { test } = require('node:test');
3+
4+
test('fail', () => {
5+
assert.fail('a.mjs fail');
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const assert = require('node:assert');
2+
const { test } = require('node:test');
3+
4+
test('fail', () => {
5+
assert.fail('b.mjs fail');
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
require('../common');
4+
const fixtures = require('../common/fixtures');
5+
const assert = require('node:assert');
6+
const { spawnSync } = require('node:child_process');
7+
const { test } = require('node:test');
8+
const cwd = fixtures.path('test-runner', 'error-reporter-fail-fast');
9+
10+
test('all tests failures reported without FAIL_FAST flag', async () => {
11+
const args = [
12+
'--test-reporter=./test/common/test-error-reporter.js',
13+
'--test-concurrency=1',
14+
'--test',
15+
`${cwd}/*.mjs`,
16+
];
17+
const cp = spawnSync(process.execPath, args);
18+
const failureCount = (cp.stdout.toString().match(/Test failure:/g) || []).length;
19+
assert.strictEqual(failureCount, 2);
20+
});
21+
22+
test('FAIL_FAST stops test execution after first failure', async () => {
23+
const args = [
24+
'--test-reporter=./test/common/test-error-reporter.js',
25+
'--test-concurrency=1',
26+
'--test',
27+
`${cwd}/*.mjs`,
28+
];
29+
const cp = spawnSync(process.execPath, args, { env: { ...process.env, FAIL_FAST: 'true' } });
30+
const failureCount = (cp.stdout.toString().match(/Test failure:/g) || []).length;
31+
assert.strictEqual(failureCount, 1);
32+
});

0 commit comments

Comments
 (0)