Skip to content

Commit 0dbb8a7

Browse files
tims-bsquarerichardlau
authored andcommitted
lib: allow checking the test result from afterEach
PR-URL: #51485 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent abda8af commit 0dbb8a7

File tree

5 files changed

+107
-14
lines changed

5 files changed

+107
-14
lines changed

lib/internal/test_runner/test.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ class TestContext {
132132
return this.#test.name;
133133
}
134134

135+
get error() {
136+
return this.#test.error;
137+
}
138+
139+
get passed() {
140+
return this.#test.passed;
141+
}
142+
135143
diagnostic(message) {
136144
this.#test.diagnostic(message);
137145
}
@@ -637,12 +645,17 @@ class Test extends AsyncResource {
637645
return;
638646
}
639647

640-
await afterEach();
641-
await after();
642648
this.pass();
649+
try {
650+
await afterEach();
651+
await after();
652+
} catch (err) {
653+
// If one of the after hooks has thrown unset endTime so that the
654+
// catch below can do its cancel/fail logic.
655+
this.endTime = null;
656+
throw err;
657+
}
643658
} catch (err) {
644-
try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ }
645-
try { await after(); } catch { /* Ignore error. */ }
646659
if (isTestFailureError(err)) {
647660
if (err.failureType === kTestTimeoutFailure) {
648661
this.#cancel(err);
@@ -652,6 +665,8 @@ class Test extends AsyncResource {
652665
} else {
653666
this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
654667
}
668+
try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ }
669+
try { await after(); } catch { /* Ignore error. */ }
655670
} finally {
656671
stopPromise?.[SymbolDispose]();
657672

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

+19
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,25 @@ test('afterEach when test fails', async (t) => {
152152
await t.test('2', () => {});
153153
});
154154

155+
test('afterEach context when test passes', async (t) => {
156+
t.afterEach(common.mustCall((ctx) => {
157+
assert.strictEqual(ctx.name, '1');
158+
assert.strictEqual(ctx.passed, true);
159+
assert.strictEqual(ctx.error, null);
160+
}));
161+
await t.test('1', () => {});
162+
});
163+
164+
test('afterEach context when test fails', async (t) => {
165+
const err = new Error('test');
166+
t.afterEach(common.mustCall((ctx) => {
167+
assert.strictEqual(ctx.name, '1');
168+
assert.strictEqual(ctx.passed, false);
169+
assert.strictEqual(ctx.error, err);
170+
}));
171+
await t.test('1', () => { throw err });
172+
});
173+
155174
test('afterEach throws and test fails', async (t) => {
156175
t.after(common.mustCall());
157176
t.afterEach(() => { throw new Error('afterEach'); });

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

+42-7
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,41 @@ not ok 12 - afterEach when test fails
505505
error: '1 subtest failed'
506506
code: 'ERR_TEST_FAILURE'
507507
...
508+
# Subtest: afterEach context when test passes
509+
# Subtest: 1
510+
ok 1 - 1
511+
---
512+
duration_ms: *
513+
...
514+
1..1
515+
ok 13 - afterEach context when test passes
516+
---
517+
duration_ms: *
518+
...
519+
# Subtest: afterEach context when test fails
520+
# Subtest: 1
521+
not ok 1 - 1
522+
---
523+
duration_ms: *
524+
location: '/test/fixtures/test-runner/output/hooks.js:(LINE):11'
525+
failureType: 'testCodeFailure'
526+
error: 'test'
527+
code: 'ERR_TEST_FAILURE'
528+
stack: |-
529+
*
530+
*
531+
*
532+
*
533+
...
534+
1..1
535+
not ok 14 - afterEach context when test fails
536+
---
537+
duration_ms: *
538+
location: '/test/fixtures/test-runner/output/hooks.js:(LINE):1'
539+
failureType: 'subtestsFailed'
540+
error: '1 subtest failed'
541+
code: 'ERR_TEST_FAILURE'
542+
...
508543
# Subtest: afterEach throws and test fails
509544
# Subtest: 1
510545
not ok 1 - 1
@@ -546,7 +581,7 @@ not ok 12 - afterEach when test fails
546581
*
547582
...
548583
1..2
549-
not ok 13 - afterEach throws and test fails
584+
not ok 15 - afterEach throws and test fails
550585
---
551586
duration_ms: *
552587
location: '/test/fixtures/test-runner/output/hooks.js:(LINE):1'
@@ -555,7 +590,7 @@ not ok 13 - afterEach throws and test fails
555590
code: 'ERR_TEST_FAILURE'
556591
...
557592
# Subtest: t.after() is called if test body throws
558-
not ok 14 - t.after() is called if test body throws
593+
not ok 16 - t.after() is called if test body throws
559594
---
560595
duration_ms: *
561596
location: '/test/fixtures/test-runner/output/hooks.js:(LINE):1'
@@ -580,7 +615,7 @@ not ok 14 - t.after() is called if test body throws
580615
code: 'ERR_TEST_FAILURE'
581616
...
582617
1..1
583-
not ok 15 - run after when before throws
618+
not ok 17 - run after when before throws
584619
---
585620
duration_ms: *
586621
type: 'suite'
@@ -599,15 +634,15 @@ not ok 15 - run after when before throws
599634
*
600635
*
601636
...
602-
1..15
637+
1..17
603638
# before 1 called
604639
# before 2 called
605640
# after 1 called
606641
# after 2 called
607-
# tests 39
642+
# tests 43
608643
# suites 9
609-
# pass 14
610-
# fail 22
644+
# pass 16
645+
# fail 24
611646
# cancelled 3
612647
# skipped 0
613648
# todo 0

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

+25-3
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,20 @@
258258
2 (*ms)
259259
afterEach when test fails (*ms)
260260

261+
afterEach context when test passes
262+
1 (*ms)
263+
afterEach context when test passes (*ms)
264+
265+
afterEach context when test fails
266+
1 (*ms)
267+
Error: test
268+
*
269+
*
270+
*
271+
*
272+
273+
afterEach context when test fails (*ms)
274+
261275
afterEach throws and test fails
262276
1 (*ms)
263277
Error: test
@@ -315,10 +329,10 @@
315329
before 2 called
316330
after 1 called
317331
after 2 called
318-
tests 39
332+
tests 43
319333
suites 9
320-
pass 14
321-
fail 22
334+
pass 16
335+
fail 24
322336
cancelled 3
323337
skipped 0
324338
todo 0
@@ -551,6 +565,14 @@
551565
*
552566
*
553567

568+
*
569+
1 (*ms)
570+
Error: test
571+
*
572+
*
573+
*
574+
*
575+
554576
*
555577
1 (*ms)
556578
Error: test

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

+2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ true !== false
155155
<testcase name="+sync throw fail" time="*" classname="test" failure="thrown from subtest sync throw fail">
156156
<failure type="testCodeFailure" message="thrown from subtest sync throw fail">
157157
Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fail
158+
*
158159
* {
159160
code: 'ERR_TEST_FAILURE',
160161
failureType: 'testCodeFailure',
@@ -338,6 +339,7 @@ Error [ERR_TEST_FAILURE]: thrown from callback async throw
338339
<testcase name="sync throw fails at first" time="*" classname="test" failure="thrown from subtest sync throw fails at first">
339340
<failure type="testCodeFailure" message="thrown from subtest sync throw fails at first">
340341
Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at first
342+
*
341343
* {
342344
code: 'ERR_TEST_FAILURE',
343345
failureType: 'testCodeFailure',

0 commit comments

Comments
 (0)