Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4b583bf

Browse filesBrowse files
tims-bsquaretargos
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 391aeb1 commit 4b583bf
Copy full SHA for 4b583bf

File tree

Expand file treeCollapse file tree

5 files changed

+107
-14
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+107
-14
lines changed
Open diff view settings
Collapse file

‎lib/internal/test_runner/test.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/test.js
+19-4Lines changed: 19 additions & 4 deletions
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
}
@@ -639,12 +647,17 @@ class Test extends AsyncResource {
639647
return;
640648
}
641649

642-
await afterEach();
643-
await after();
644650
this.pass();
651+
try {
652+
await afterEach();
653+
await after();
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+
throw err;
659+
}
645660
} catch (err) {
646-
try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ }
647-
try { await after(); } 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(new ERR_TEST_FAILURE(err, kTestCodeFailure));
656669
}
670+
try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ }
671+
try { await after(); } catch { /* Ignore error. */ }
657672
} finally {
658673
stopPromise?.[SymbolDispose]();
659674

Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/test-runner/output/hooks.js
+19Lines 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
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'); });
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/test-runner/output/hooks.snapshot
+42-7Lines 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
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/test-runner/output/hooks_spec_reporter.snapshot
+25-3Lines 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
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/test-runner/output/junit_reporter.snapshot
+2Lines changed: 2 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.