Skip to content

Commit c80e426

Browse files
cjihrigMoLow
authored andcommitted
fix: run t.after() if test body throws
This commit fixes a bug where t.after() was not called if the test body threw an exception. PR-URL: nodejs/node#45870 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Matteo Collina <[email protected]> (cherry picked from commit 385d595a4f1d887f6d4221e6071571132498d57c)
1 parent d1343a7 commit c80e426

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

lib/internal/test_runner/test.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/232efb06fe8787e9573e298ce7ac293ad23b7684/lib/internal/test_runner/test.js
1+
// https://github.com/nodejs/node/blob/385d595a4f1d887f6d4221e6071571132498d57c/lib/internal/test_runner/test.js
22

33
'use strict'
44

@@ -496,6 +496,11 @@ class Test extends AsyncResource {
496496
}
497497

498498
const { args, ctx } = this.getRunArgs()
499+
const after = runOnce(async () => {
500+
if (this.hooks.after.length > 0) {
501+
await this.runHook('after', { args, ctx })
502+
}
503+
})
499504
const afterEach = runOnce(async () => {
500505
if (this.parent?.hooks.afterEach.length > 0) {
501506
await this.parent.runHook('afterEach', { args, ctx })
@@ -537,10 +542,11 @@ class Test extends AsyncResource {
537542
return
538543
}
539544

540-
await this.runHook('after', { args, ctx })
545+
await after()
541546
await afterEach()
542547
this.pass()
543548
} catch (err) {
549+
try { await after() } catch { /* Ignore error. */ }
544550
try { await afterEach() } catch { /* test is already failing, let's the error */ }
545551
if (isTestFailureError(err)) {
546552
if (err.failureType === kTestTimeoutFailure) {

test/message/test_runner_hooks.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/215c5317d4837287fddb2e3b97872babd53183ac/test/message/test_runner_hooks.js
1+
// https://github.com/nodejs/node/blob/385d595a4f1d887f6d4221e6071571132498d57c/test/message/test_runner_hooks.js
22
// Flags: --no-warnings
33
'use strict'
44
const common = require('../common')
@@ -142,3 +142,10 @@ test('afterEach throws and test fails', async (t) => {
142142
await t.test('1', () => { throw new Error('test') })
143143
await t.test('2', () => {})
144144
})
145+
146+
test('t.after() is called if test body throws', (t) => {
147+
t.after(() => {
148+
t.diagnostic('- after() called')
149+
})
150+
throw new Error('bye')
151+
})

test/message/test_runner_hooks.out

+21-3
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,28 @@ not ok 12 - afterEach throws and test fails
475475
error: '2 subtests failed'
476476
code: 'ERR_TEST_FAILURE'
477477
...
478-
1..12
479-
# tests 12
478+
# Subtest: t.after() is called if test body throws
479+
not ok 13 - t.after() is called if test body throws
480+
---
481+
duration_ms: *
482+
failureType: 'testCodeFailure'
483+
error: 'bye'
484+
code: 'ERR_TEST_FAILURE'
485+
stack: |-
486+
*
487+
*
488+
*
489+
*
490+
*
491+
*
492+
*
493+
*
494+
...
495+
# - after() called
496+
1..13
497+
# tests 13
480498
# pass 2
481-
# fail 10
499+
# fail 11
482500
# cancelled 0
483501
# skipped 0
484502
# todo 0

0 commit comments

Comments
 (0)