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 6a82fbd

Browse filesBrowse files
committed
test_runner: fix global after hook
PR-URL: #48231 Fixes: #48230 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 4d1c38b commit 6a82fbd
Copy full SHA for 6a82fbd

File tree

Expand file treeCollapse file tree

4 files changed

+29
-21
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+29
-21
lines changed
Open diff view settings
Collapse file

‎lib/internal/test_runner/harness.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/harness.js
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ function setup(root) {
138138
const rejectionHandler =
139139
createProcessEventHandler('unhandledRejection', root);
140140
const coverage = configureCoverage(root, globalOptions);
141-
const exitHandler = () => {
142-
root.postRun(new ERR_TEST_FAILURE(
141+
const exitHandler = async () => {
142+
await root.run(new ERR_TEST_FAILURE(
143143
'Promise resolution is still pending but the event loop has already resolved',
144144
kCancelledByParent));
145145

@@ -148,8 +148,8 @@ function setup(root) {
148148
process.removeListener('uncaughtException', exceptionHandler);
149149
};
150150

151-
const terminationHandler = () => {
152-
exitHandler();
151+
const terminationHandler = async () => {
152+
await exitHandler();
153153
process.exit();
154154
};
155155

Collapse file

‎lib/internal/test_runner/test.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/test.js
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ class Test extends AsyncResource {
427427
validateOneOf(name, 'hook name', kHookNames);
428428
// eslint-disable-next-line no-use-before-define
429429
const hook = new TestHook(fn, options);
430-
if (name === 'before') {
430+
if (name === 'before' || name === 'after') {
431431
hook.run = runOnce(hook.run);
432432
}
433433
ArrayPrototypePush(this.hooks[name], hook);
@@ -514,7 +514,7 @@ class Test extends AsyncResource {
514514
}
515515
}
516516

517-
async run() {
517+
async run(pendingSubtestsError) {
518518
if (this.parent !== null) {
519519
this.parent.activeSubtests++;
520520
}
@@ -526,11 +526,11 @@ class Test extends AsyncResource {
526526
}
527527

528528
const { args, ctx } = this.getRunArgs();
529-
const after = runOnce(async () => {
529+
const after = async () => {
530530
if (this.hooks.after.length > 0) {
531531
await this.runHook('after', { args, ctx });
532532
}
533-
});
533+
};
534534
const afterEach = runOnce(async () => {
535535
if (this.parent?.hooks.afterEach.length > 0) {
536536
await this.parent.runHook('afterEach', { args, ctx });
@@ -579,8 +579,8 @@ class Test extends AsyncResource {
579579
await after();
580580
this.pass();
581581
} catch (err) {
582-
try { await after(); } catch { /* Ignore error. */ }
583582
try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ }
583+
try { await after(); } catch { /* Ignore error. */ }
584584
if (isTestFailureError(err)) {
585585
if (err.failureType === kTestTimeoutFailure) {
586586
this.#cancel(err);
@@ -594,7 +594,7 @@ class Test extends AsyncResource {
594594

595595
// Clean up the test. Then, try to report the results and execute any
596596
// tests that were pending due to available concurrency.
597-
this.postRun();
597+
this.postRun(pendingSubtestsError);
598598
}
599599

600600
postRun(pendingSubtestsError) {
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/test-runner/output/hooks.js
+16-11Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const assert = require('assert');
55
const { test, describe, it, before, after, beforeEach, afterEach } = require('node:test');
66

77
before((t) => t.diagnostic('before 1 called'));
8+
after((t) => t.diagnostic('after 1 called'));
89

910
describe('describe hooks', () => {
1011
const testArr = [];
@@ -107,17 +108,20 @@ test('test hooks', async (t) => {
107108
await t.test('nested 2', () => testArr.push('nested 2'));
108109
});
109110

110-
assert.deepStrictEqual(testArr, [
111-
'before test hooks',
112-
'beforeEach 1', '1', 'afterEach 1',
113-
'beforeEach 2', '2', 'afterEach 2',
114-
'beforeEach nested',
115-
'nested before nested',
116-
'beforeEach nested 1', 'nested beforeEach nested 1', 'nested1', 'afterEach nested 1', 'nested afterEach nested 1',
117-
'beforeEach nested 2', 'nested beforeEach nested 2', 'nested 2', 'afterEach nested 2', 'nested afterEach nested 2',
118-
'afterEach nested',
119-
'nested after nested',
120-
]);
111+
t.after(common.mustCall(() => {
112+
assert.deepStrictEqual(testArr, [
113+
'before test hooks',
114+
'beforeEach 1', '1', 'afterEach 1',
115+
'beforeEach 2', '2', 'afterEach 2',
116+
'beforeEach nested',
117+
'nested before nested',
118+
'beforeEach nested 1', 'nested beforeEach nested 1', 'nested1', 'afterEach nested 1', 'nested afterEach nested 1',
119+
'beforeEach nested 2', 'nested beforeEach nested 2', 'nested 2', 'afterEach nested 2', 'nested afterEach nested 2',
120+
'afterEach nested',
121+
'nested after nested',
122+
'after test hooks',
123+
]);
124+
}));
121125
});
122126

123127
test('t.before throws', async (t) => {
@@ -164,3 +168,4 @@ test('t.after() is called if test body throws', (t) => {
164168
});
165169

166170
before((t) => t.diagnostic('before 2 called'));
171+
after((t) => t.diagnostic('after 2 called'));
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/test-runner/output/hooks.snapshot
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ not ok 3 - after throws
9797
*
9898
*
9999
*
100+
*
100101
...
101102
# Subtest: beforeEach throws
102103
# Subtest: 1
@@ -544,6 +545,8 @@ not ok 14 - t.after() is called if test body throws
544545
1..14
545546
# before 1 called
546547
# before 2 called
548+
# after 1 called
549+
# after 2 called
547550
# tests 38
548551
# suites 8
549552
# pass 14

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.