Skip to content

Commit da17a25

Browse files
pulkit-30richardlau
authored andcommitted
test_runner: fixed to run after hook if before throws an error
PR-URL: #51062 Refs: #50842 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Raz Luvaton <[email protected]>
1 parent cbda4e9 commit da17a25

File tree

4 files changed

+84
-8
lines changed

4 files changed

+84
-8
lines changed

lib/internal/test_runner/test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,7 @@ class Suite extends Test {
924924
const hookArgs = this.getRunArgs();
925925

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

948949
await SafePromiseRace([promise, stopPromise]);
949-
await this.runHook('after', hookArgs);
950+
await after();
950951

951952
this.pass();
952953
} catch (err) {
954+
try { await after(); } catch { /* suite is already failing */ }
953955
if (isTestFailureError(err)) {
954956
this.fail(err);
955957
} else {

test/fixtures/test-runner/output/hooks.js

+8
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,13 @@ test('t.after() is called if test body throws', (t) => {
166166
throw new Error('bye');
167167
});
168168

169+
describe('run after when before throws', () => {
170+
after(common.mustCall(() => {
171+
console.log("- after() called")
172+
}));
173+
before(() => { throw new Error('before')});
174+
it('1', () => {});
175+
});
176+
169177
before((t) => t.diagnostic('before 2 called'));
170178
after((t) => t.diagnostic('after 2 called'));

test/fixtures/test-runner/output/hooks.snapshot

+35-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- after() called
12
TAP version 13
23
# Subtest: describe hooks
34
# Subtest: 1
@@ -568,16 +569,46 @@ not ok 14 - t.after() is called if test body throws
568569
*
569570
...
570571
# - after() called
571-
1..14
572+
# Subtest: run after when before throws
573+
# Subtest: 1
574+
not ok 1 - 1
575+
---
576+
duration_ms: *
577+
location: '/test/fixtures/test-runner/output/hooks.js:(LINE):3'
578+
failureType: 'cancelledByParent'
579+
error: 'test did not finish before its parent and was cancelled'
580+
code: 'ERR_TEST_FAILURE'
581+
...
582+
1..1
583+
not ok 15 - run after when before throws
584+
---
585+
duration_ms: *
586+
type: 'suite'
587+
location: '/test/fixtures/test-runner/output/hooks.js:(LINE):1'
588+
failureType: 'hookFailed'
589+
error: 'before'
590+
code: 'ERR_TEST_FAILURE'
591+
stack: |-
592+
*
593+
*
594+
*
595+
*
596+
*
597+
*
598+
*
599+
*
600+
*
601+
...
602+
1..15
572603
# before 1 called
573604
# before 2 called
574605
# after 1 called
575606
# after 2 called
576-
# tests 38
577-
# suites 8
607+
# tests 39
608+
# suites 9
578609
# pass 14
579610
# fail 22
580-
# cancelled 2
611+
# cancelled 3
581612
# skipped 0
582613
# todo 0
583614
# duration_ms *

test/fixtures/test-runner/output/hooks_spec_reporter.snapshot

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- after() called
12
describe hooks
23
1 (*ms)
34
2 (*ms)
@@ -293,15 +294,32 @@
293294
*
294295

295296
- after() called
297+
run after when before throws
298+
1
299+
'test did not finish before its parent and was cancelled'
300+
301+
run after when before throws (*ms)
302+
303+
Error: before
304+
*
305+
*
306+
*
307+
*
308+
*
309+
*
310+
*
311+
*
312+
*
313+
296314
before 1 called
297315
before 2 called
298316
after 1 called
299317
after 2 called
300-
tests 38
301-
suites 8
318+
tests 39
319+
suites 9
302320
pass 14
303321
fail 22
304-
cancelled 2
322+
cancelled 3
305323
skipped 0
306324
todo 0
307325
duration_ms *
@@ -567,3 +585,20 @@
567585
*
568586
*
569587
*
588+
589+
*
590+
1
591+
'test did not finish before its parent and was cancelled'
592+
593+
*
594+
run after when before throws (*ms)
595+
Error: before
596+
*
597+
*
598+
*
599+
*
600+
*
601+
*
602+
*
603+
*
604+
*

0 commit comments

Comments
 (0)