Skip to content

Commit d755f1a

Browse files
authored
lib: allow checking the test result from afterEach
PR-URL: #51485 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Moshe Atlow <[email protected]>
1 parent fbf1fb3 commit d755f1a

File tree

5 files changed

+107
-14
lines changed

5 files changed

+107
-14
lines changed

‎lib/internal/test_runner/test.js‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ class TestContext{
132132
returnthis.#test.name;
133133
}
134134

135+
geterror(){
136+
returnthis.#test.error;
137+
}
138+
139+
getpassed(){
140+
returnthis.#test.passed;
141+
}
142+
135143
diagnostic(message){
136144
this.#test.diagnostic(message);
137145
}
@@ -639,12 +647,17 @@ class Test extends AsyncResource{
639647
return;
640648
}
641649

642-
awaitafterEach();
643-
awaitafter();
644650
this.pass();
651+
try{
652+
awaitafterEach();
653+
awaitafter();
654+
}catch(err){
655+
// If one of the after hooks has thrown unset endTime so that the
656+
// catch below can do its cancel/fail logic.
657+
this.endTime=null;
658+
throwerr;
659+
}
645660
}catch(err){
646-
try{awaitafterEach();}catch{/* test is already failing, let's ignore the error */}
647-
try{awaitafter();}catch{/* Ignore error. */}
648661
if(isTestFailureError(err)){
649662
if(err.failureType===kTestTimeoutFailure){
650663
this.#cancel(err);
@@ -654,6 +667,8 @@ class Test extends AsyncResource{
654667
}else{
655668
this.fail(newERR_TEST_FAILURE(err,kTestCodeFailure));
656669
}
670+
try{awaitafterEach();}catch{/* test is already failing, let's ignore the error */}
671+
try{awaitafter();}catch{/* Ignore error. */}
657672
}finally{
658673
stopPromise?.[SymbolDispose]();
659674

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,25 @@ test('afterEach when test fails', async (t) =>{
152152
awaitt.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+
awaitt.test('1',()=>{});
162+
});
163+
164+
test('afterEach context when test fails',async(t)=>{
165+
consterr=newError('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+
awaitt.test('1',()=>{throwerr});
172+
});
173+
155174
test('afterEach throws and test fails',async(t)=>{
156175
t.after(common.mustCall());
157176
t.afterEach(()=>{thrownewError('afterEach');});

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

Lines changed: 42 additions & 7 deletions
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‎

Lines changed: 25 additions & 3 deletions
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‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ true !== false
155155
<testcasename="+sync throw fail"time="*"classname="test"failure="thrown from subtest sync throw fail">
156156
<failuretype="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
<testcasename="sync throw fails at first"time="*"classname="test"failure="thrown from subtest sync throw fails at first">
339340
<failuretype="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)