Skip to content

Commit 5fdf374

Browse files
fossamagnajuanarbol
authored andcommitted
test_runner: avoid swallowing of asynchronously thrown errors
Fixes: #44612 PR-URL: #45264 Backport-PR-URL: #46839 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent c04808d commit 5fdf374

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

doc/api/test.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ top level of the file's TAP output.
272272

273273
The second `setImmediate()` creates an `uncaughtException` event.
274274
`uncaughtException` and `unhandledRejection` events originating from a completed
275-
test are handled by the `test` module and reported as diagnostic warnings in
276-
the top level of the file's TAP output.
275+
test are marked as failed by the `test` module and reported as diagnostic
276+
warnings in the top level of the file's TAP output.
277277

278278
```js
279279
test('a test that creates asynchronous activity', (t) => {

lib/internal/test_runner/harness.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function createProcessEventHandler(eventName, rootTest) {
4444
`triggered an ${eventName} event.`;
4545

4646
rootTest.diagnostic(msg);
47+
process.exitCode = 1;
4748
return;
4849
}
4950

test/es-module/test-esm-repl-imports.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { describe, it } = require('node:test');
99

1010

1111
describe('ESM: REPL runs', { concurrency: true }, () => {
12-
it((context, done) => {
12+
it((done) => {
1313
const child = spawn(execPath, [
1414
'--interactive',
1515
], {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import test from 'node:test';
2+
3+
test('extraneous async activity test', () => {
4+
setImmediate(() => { throw new Error(); });
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import test from 'node:test';
2+
3+
test('extraneous async activity test', () => {
4+
setTimeout(() => { throw new Error(); }, 100);
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
require('../common');
3+
const fixtures = require('../common/fixtures');
4+
const assert = require('assert');
5+
const { spawnSync } = require('child_process');
6+
7+
{
8+
const child = spawnSync(process.execPath, [
9+
'--test',
10+
fixtures.path('test-runner', 'extraneous_set_immediate_async.mjs'),
11+
]);
12+
const stdout = child.stdout.toString();
13+
assert.match(stdout, /^# pass 0$/m);
14+
assert.match(stdout, /^# fail 1$/m);
15+
assert.match(stdout, /^# cancelled 0$/m);
16+
assert.strictEqual(child.status, 1);
17+
assert.strictEqual(child.signal, null);
18+
}
19+
20+
{
21+
const child = spawnSync(process.execPath, [
22+
'--test',
23+
fixtures.path('test-runner', 'extraneous_set_timeout_async.mjs'),
24+
]);
25+
const stdout = child.stdout.toString();
26+
assert.match(stdout, /^# pass 0$/m);
27+
assert.match(stdout, /^# fail 1$/m);
28+
assert.match(stdout, /^# cancelled 0$/m);
29+
assert.strictEqual(child.status, 1);
30+
assert.strictEqual(child.signal, null);
31+
}

0 commit comments

Comments
 (0)