Skip to content

Commit cff397a

Browse files
fossamagnaMoLow
authored andcommitted
fix: avoid swallowing of asynchronously thrown errors
Fixes: nodejs/node#44612 PR-URL: nodejs/node#45264 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> (cherry picked from commit 06603c44a5b0e92b1a3591ace467ce9770bf9658)
1 parent f2815af commit cff397a

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ top level of the file's TAP output.
279279

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

285285
```js
286286
test('a test that creates asynchronous activity', t => {

lib/internal/test_runner/harness.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/59527de13d39327eb3dfa8dedc92241eb40066d5/lib/internal/test_runner/harness.js
1+
// https://github.com/nodejs/node/blob/06603c44a5b0e92b1a3591ace467ce9770bf9658/lib/internal/test_runner/harness.js
22
'use strict'
33
const {
44
ArrayPrototypeForEach,
@@ -47,6 +47,7 @@ function createProcessEventHandler (eventName, rootTest) {
4747
`triggered an ${eventName} event.`
4848

4949
rootTest.diagnostic(msg)
50+
process.exitCode = 1
5051
return
5152
}
5253

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// https://github.com/nodejs/node/blob/06603c44a5b0e92b1a3591ace467ce9770bf9658/test/fixtures/test-runner/extraneous_set_immediate_async.mjs
2+
import test from '#node:test'
3+
4+
test('extraneous async activity test', () => {
5+
setImmediate(() => { throw new Error() })
6+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// https://github.com/nodejs/node/blob/06603c44a5b0e92b1a3591ace467ce9770bf9658/test/fixtures/test-runner/extraneous_set_timeout_async.mjs
2+
import test from '#node:test'
3+
4+
test('extraneous async activity test', () => {
5+
setTimeout(() => { throw new Error() }, 100)
6+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// https://github.com/nodejs/node/blob/06603c44a5b0e92b1a3591ace467ce9770bf9658/test/parallel/test-runner-extraneous-async-activity.js
2+
'use strict'
3+
require('../common')
4+
const fixtures = require('../common/fixtures')
5+
const assert = require('assert')
6+
const { spawnSync } = require('child_process')
7+
8+
{
9+
const child = spawnSync(process.execPath, [
10+
'--test',
11+
fixtures.path('test-runner', 'extraneous_set_immediate_async.mjs')
12+
])
13+
const stdout = child.stdout.toString()
14+
assert.match(stdout, /^# pass 0$/m)
15+
assert.match(stdout, /^# fail 1$/m)
16+
assert.match(stdout, /^# cancelled 0$/m)
17+
assert.strictEqual(child.status, 1)
18+
assert.strictEqual(child.signal, null)
19+
}
20+
21+
{
22+
const child = spawnSync(process.execPath, [
23+
'--test',
24+
fixtures.path('test-runner', 'extraneous_set_timeout_async.mjs')
25+
])
26+
const stdout = child.stdout.toString()
27+
assert.match(stdout, /^# pass 0$/m)
28+
assert.match(stdout, /^# fail 1$/m)
29+
assert.match(stdout, /^# cancelled 0$/m)
30+
assert.strictEqual(child.status, 1)
31+
assert.strictEqual(child.signal, null)
32+
}

0 commit comments

Comments
 (0)