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: fix after hook inside test suite is not executed if before() throws an error #51062

Merged
merged 2 commits into from
Dec 17, 2023
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
4 changes: 3 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@ class Suite extends Test {
const hookArgs = this.getRunArgs();

let stopPromise;
const after = runOnce(() => this.runHook('after', hookArgs));
try {
this.parent.activeSubtests++;
await this.buildSuite;
Expand All @@ -946,10 +947,11 @@ class Suite extends Test {
const promise = SafePromiseAll(subtests, (subtests) => subtests.start());

await SafePromiseRace([promise, stopPromise]);
await this.runHook('after', hookArgs);
await after();

this.pass();
} catch (err) {
try { await after(); } catch { /* suite is already failing */ }
if (isTestFailureError(err)) {
this.fail(err);
} else {
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/test-runner/output/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,13 @@ test('t.after() is called if test body throws', (t) => {
throw new Error('bye');
});

describe('run after when before throws', () => {
after(common.mustCall(() => {
console.log("- after() called")
}));
pulkit-30 marked this conversation as resolved.
Show resolved Hide resolved
before(() => { throw new Error('before')});
it('1', () => {});
});

before((t) => t.diagnostic('before 2 called'));
after((t) => t.diagnostic('after 2 called'));
39 changes: 35 additions & 4 deletions test/fixtures/test-runner/output/hooks.snapshot
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- after() called
TAP version 13
# Subtest: describe hooks
# Subtest: 1
Expand Down Expand Up @@ -568,16 +569,46 @@ not ok 14 - t.after() is called if test body throws
*
...
# - after() called
1..14
# Subtest: run after when before throws
# Subtest: 1
not ok 1 - 1
---
duration_ms: *
location: '/test/fixtures/test-runner/output/hooks.js:(LINE):3'
failureType: 'cancelledByParent'
error: 'test did not finish before its parent and was cancelled'
code: 'ERR_TEST_FAILURE'
...
1..1
not ok 15 - run after when before throws
---
duration_ms: *
type: 'suite'
location: '/test/fixtures/test-runner/output/hooks.js:(LINE):1'
failureType: 'hookFailed'
error: 'before'
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
*
...
1..15
# before 1 called
# before 2 called
# after 1 called
# after 2 called
# tests 38
# suites 8
# tests 39
# suites 9
# pass 14
# fail 22
# cancelled 2
# cancelled 3
# skipped 0
# todo 0
# duration_ms *
41 changes: 38 additions & 3 deletions test/fixtures/test-runner/output/hooks_spec_reporter.snapshot
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- after() called
describe hooks
1 (*ms)
2 (*ms)
Expand Down Expand Up @@ -293,15 +294,32 @@
*

- after() called
run after when before throws
1
'test did not finish before its parent and was cancelled'

run after when before throws (*ms)

Error: before
*
*
*
*
*
*
*
*
*

before 1 called
before 2 called
after 1 called
after 2 called
tests 38
suites 8
tests 39
suites 9
pass 14
fail 22
cancelled 2
cancelled 3
skipped 0
todo 0
duration_ms *
Expand Down Expand Up @@ -567,3 +585,20 @@
*
*
*

*
1
'test did not finish before its parent and was cancelled'

*
run after when before throws (*ms)
Error: before
*
*
*
*
*
*
*
*
*