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 4737314

Browse filesBrowse files
philnashMoLow
authored andcommitted
test_runner: fix ordering of test hooks
For tests with subtests the before hook was being run after the beforeEach hook, which is the opposite to test suites and expectations. Also, a function was being used to close over the after hooks, but at the point it was being run the after hooks were not yet set up. Fixes #47915 PR-URL: #47931 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 4bece05 commit 4737314
Copy full SHA for 4737314

File tree

Expand file treeCollapse file tree

2 files changed

+10
-5
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+10
-5
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
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,12 @@ class Test extends AsyncResource {
537537
});
538538

539539
try {
540-
if (this.parent?.hooks.beforeEach.length > 0) {
541-
await this.parent.runHook('beforeEach', { args, ctx });
542-
}
543540
if (this.parent?.hooks.before.length > 0) {
544541
await this.parent.runHook('before', this.parent.getRunArgs());
545542
}
543+
if (this.parent?.hooks.beforeEach.length > 0) {
544+
await this.parent.runHook('beforeEach', { args, ctx });
545+
}
546546
const stopPromise = stopTest(this.timeout, this.signal);
547547
const runArgs = ArrayPrototypeSlice(args);
548548
ArrayPrototypeUnshift(runArgs, this.fn, ctx);
@@ -574,8 +574,8 @@ class Test extends AsyncResource {
574574
return;
575575
}
576576

577-
await after();
578577
await afterEach();
578+
await after();
579579
this.pass();
580580
} catch (err) {
581581
try { await after(); } catch { /* Ignore error. */ }
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/test-runner/output/hooks.js
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,24 @@ test('test hooks', async (t) => {
9999
await t.test('2', () => testArr.push('2'));
100100

101101
await t.test('nested', async (t) => {
102+
t.before((t) => testArr.push('nested before ' + t.name));
103+
t.after((t) => testArr.push('nested after ' + t.name));
102104
t.beforeEach((t) => testArr.push('nested beforeEach ' + t.name));
103105
t.afterEach((t) => testArr.push('nested afterEach ' + t.name));
104106
await t.test('nested 1', () => testArr.push('nested1'));
105107
await t.test('nested 2', () => testArr.push('nested 2'));
106108
});
107109

108110
assert.deepStrictEqual(testArr, [
109-
'beforeEach 1', 'before test hooks', '1', 'afterEach 1',
111+
'before test hooks',
112+
'beforeEach 1', '1', 'afterEach 1',
110113
'beforeEach 2', '2', 'afterEach 2',
111114
'beforeEach nested',
115+
'nested before nested',
112116
'beforeEach nested 1', 'nested beforeEach nested 1', 'nested1', 'afterEach nested 1', 'nested afterEach nested 1',
113117
'beforeEach nested 2', 'nested beforeEach nested 2', 'nested 2', 'afterEach nested 2', 'nested afterEach nested 2',
114118
'afterEach nested',
119+
'nested after nested',
115120
]);
116121
});
117122

0 commit comments

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