Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: run t.after() if test body throws #45870

Merged
merged 1 commit into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ class Test extends AsyncResource {
}

const { args, ctx } = this.getRunArgs();
const after = runOnce(async () => {
if (this.hooks.after.length > 0) {
await this.runHook('after', { args, ctx });
}
});
const afterEach = runOnce(async () => {
if (this.parent?.hooks.afterEach.length > 0) {
await this.parent.runHook('afterEach', { args, ctx });
Expand Down Expand Up @@ -549,10 +554,11 @@ class Test extends AsyncResource {
return;
}

await this.runHook('after', { args, ctx });
await after();
await afterEach();
this.pass();
} catch (err) {
try { await after(); } catch { /* Ignore error. */ }
try { await afterEach(); } catch { /* test is already failing, let's the error */ }
if (isTestFailureError(err)) {
if (err.failureType === kTestTimeoutFailure) {
Expand Down
7 changes: 7 additions & 0 deletions test/message/test_runner_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,10 @@ test('afterEach throws and test fails', async (t) => {
await t.test('1', () => { throw new Error('test'); });
await t.test('2', () => {});
});

test('t.after() is called if test body throws', (t) => {
t.after(() => {
t.diagnostic('- after() called');
});
throw new Error('bye');
});
24 changes: 21 additions & 3 deletions test/message/test_runner_hooks.out
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,28 @@ not ok 12 - afterEach throws and test fails
error: '2 subtests failed'
code: 'ERR_TEST_FAILURE'
...
1..12
# tests 12
# Subtest: t.after() is called if test body throws
not ok 13 - t.after() is called if test body throws
---
duration_ms: *
failureType: 'testCodeFailure'
error: 'bye'
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
...
# - after() called
1..13
# tests 13
# pass 2
# fail 10
# fail 11
# cancelled 0
# skipped 0
# todo 0
Expand Down