Skip to content

Commit

Permalink
fixup! test: add error only reporter for node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceres6 committed Jan 2, 2025
1 parent d004f46 commit bba04dc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/common/test-error-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ module.exports = async function* errorReporter(source) {
output.push(inspect(error));
output.push('\n');
yield output.join('\n');

if (process.env.FAIL_FAST) {
yield `\n\u001b[31m✖ Bailing on failed test: ${event.data.name}\u001b[0m\n`;
process.exitCode = 1;
process.emit('SIGINT');
}
}
}
};
6 changes: 6 additions & 0 deletions test/fixtures/test-runner/error-reporter-fail-fast/a.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const assert = require('node:assert');
const { test } = require('node:test');

test('fail', () => {
assert.fail('a.mjs fail');
});
6 changes: 6 additions & 0 deletions test/fixtures/test-runner/error-reporter-fail-fast/b.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const assert = require('node:assert');
const { test } = require('node:test');

test('fail', () => {
assert.fail('b.mjs fail');
});
32 changes: 32 additions & 0 deletions test/parallel/test-runner-error-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

Check failure on line 1 in test/parallel/test-runner-error-reporter.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Mandatory module "common" must be loaded

Check failure on line 1 in test/parallel/test-runner-error-reporter.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Mandatory module "common" must be loaded before any other modules

const fixtures = require('../common/fixtures');
const assert = require('node:assert');
const { spawnSync } = require('node:child_process');
const { test } = require('node:test');
const cwd = fixtures.path('test-runner', 'error-reporter-fail-fast');
const env = { ...process.env };

test('all tests failures reported without FAIL_FAST flag', async () => {
const args = [
'--test-reporter=./test/common/test-error-reporter.js',
'--test-concurrency=1',
'--test',
`${cwd}/*.mjs`,
];
const cp = spawnSync(process.execPath, args, { env });
const failureCount = (cp.stdout.toString().match(/Test failure:/g) || []).length;
assert.strictEqual(failureCount, 2, 'Expected two test failures without FAIL_FAST');

Check failure on line 19 in test/parallel/test-runner-error-reporter.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Do not use a literal for the third argument of assert.strictEqual()
});

test('FAIL_FAST stops test execution after first failure', async () => {
const args = [
'--test-reporter=./test/common/test-error-reporter.js',
'--test-concurrency=1',
'--test',
`${cwd}/*.mjs`,
];
const cp = spawnSync(process.execPath, args, { env: { ...env, FAIL_FAST: 'true' } });
const failureCount = (cp.stdout.toString().match(/Test failure:/g) || []).length;
assert.strictEqual(failureCount, 1, 'Expected one test failure with FAIL_FAST');

Check failure on line 31 in test/parallel/test-runner-error-reporter.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Do not use a literal for the third argument of assert.strictEqual()
});

0 comments on commit bba04dc

Please sign in to comment.