Skip to content

Commit 4f3bce6

Browse files
committed
fixup! test: add error only reporter for node:test
1 parent d004f46 commit 4f3bce6

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

test/common/test-error-reporter.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ module.exports = async function* errorReporter(source) {
3030
output.push(inspect(error));
3131
output.push('\n');
3232
yield output.join('\n');
33+
34+
if (process.env.FAIL_FAST) {
35+
yield `\n\u001b[31m✖ Bailing on failed test: ${event.data.name}\u001b[0m\n`;
36+
process.exitCode = 1;
37+
process.emit('SIGINT');
38+
}
3339
}
3440
}
3541
};
Lines changed: 6 additions & 0 deletions
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+
});
Lines changed: 6 additions & 0 deletions
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+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const fixtures = require('../common/fixtures');
4+
const assert = require('node:assert');
5+
const { spawnSync } = require('node:child_process');
6+
const { test } = require('node:test');
7+
const cwd = fixtures.path('test-runner', 'error-reporter-fail-fast');
8+
const env = { ...process.env };
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, { env });
18+
const failureCount = (cp.stdout.toString().match(/Test failure:/g) || []).length;
19+
assert.strictEqual(failureCount, 2, 'Expected two test failures without FAIL_FAST');
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: { ...env, FAIL_FAST: 'true' } });
30+
const failureCount = (cp.stdout.toString().match(/Test failure:/g) || []).length;
31+
assert.strictEqual(failureCount, 1, 'Expected one test failure with FAIL_FAST');
32+
});

0 commit comments

Comments
 (0)