Skip to content

Commit

Permalink
test_runner: fixed to run after hook if before throws an error
Browse files Browse the repository at this point in the history
PR-URL: #51062
Refs: #50842
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
  • Loading branch information
pulkit-30 authored and pull[bot] committed Jan 14, 2024
1 parent 7c4b189 commit a814b8a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
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")
}));
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
*
*
*
*
*
*
*
*
*

0 comments on commit a814b8a

Please sign in to comment.