Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cade2fc

Browse filesBrowse files
cjihrigjuanarbol
authored andcommitted
test_runner: 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: #45870 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 22c1e91 commit cade2fc
Copy full SHA for cade2fc

File tree

Expand file treeCollapse file tree

3 files changed

+35
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+35
-4
lines changed
Open diff view settings
Collapse file

‎lib/internal/test_runner/test.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/test.js
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,11 @@ class Test extends AsyncResource {
508508
}
509509

510510
const { args, ctx } = this.getRunArgs();
511+
const after = runOnce(async () => {
512+
if (this.hooks.after.length > 0) {
513+
await this.runHook('after', { args, ctx });
514+
}
515+
});
511516
const afterEach = runOnce(async () => {
512517
if (this.parent?.hooks.afterEach.length > 0) {
513518
await this.parent.runHook('afterEach', { args, ctx });
@@ -549,10 +554,11 @@ class Test extends AsyncResource {
549554
return;
550555
}
551556

552-
await this.runHook('after', { args, ctx });
557+
await after();
553558
await afterEach();
554559
this.pass();
555560
} catch (err) {
561+
try { await after(); } catch { /* Ignore error. */ }
556562
try { await afterEach(); } catch { /* test is already failing, let's the error */ }
557563
if (isTestFailureError(err)) {
558564
if (err.failureType === kTestTimeoutFailure) {
Collapse file

‎test/message/test_runner_hooks.js‎

Copy file name to clipboardExpand all lines: test/message/test_runner_hooks.js
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
});
Collapse file

‎test/message/test_runner_hooks.out‎

Copy file name to clipboardExpand all lines: test/message/test_runner_hooks.out
+21-3Lines changed: 21 additions & 3 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.