Skip to content

Commit d34f649

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

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
const env = { ...process.env };
10+
11+
test('all tests failures reported without FAIL_FAST flag', async () => {
12+
const args = [
13+
'--test-reporter=./test/common/test-error-reporter.js',
14+
'--test-concurrency=1',
15+
'--test',
16+
`${cwd}/*.mjs`,
17+
];
18+
const cp = spawnSync(process.execPath, args, { env });
19+
const failureCount = (cp.stdout.toString().match(/Test failure:/g) || []).length;
20+
assert.strictEqual(failureCount, 2);
21+
});
22+
23+
test('FAIL_FAST stops test execution after first failure', async () => {
24+
const args = [
25+
'--test-reporter=./test/common/test-error-reporter.js',
26+
'--test-concurrency=1',
27+
'--test',
28+
`${cwd}/*.mjs`,
29+
];
30+
const cp = spawnSync(process.execPath, args, { env: { ...env, FAIL_FAST: 'true' } });
31+
const failureCount = (cp.stdout.toString().match(/Test failure:/g) || []).length;
32+
assert.strictEqual(failureCount, 1);
33+
});

0 commit comments

Comments
 (0)