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 afa8291

Browse filesBrowse files
cjihrigRafaelGSS
authored andcommitted
test_runner: call {before,after}Each() on suites
Prior to this commit, beforeEach() and afterEach() hooks were not called on test suites (describe()). This commit addresses that. Fixes: #45028 PR-URL: #45161 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 916e876 commit afa8291
Copy full SHA for afa8291

File tree

Expand file treeCollapse file tree

3 files changed

+15
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+15
-2
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
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,13 +731,24 @@ class Suite extends Test {
731731
}
732732

733733
const hookArgs = this.getRunArgs();
734+
735+
if (this.parent?.hooks.beforeEach.length > 0) {
736+
await this.parent[kRunHook]('beforeEach', hookArgs);
737+
}
738+
734739
await this[kRunHook]('before', hookArgs);
740+
735741
const stopPromise = stopTest(this.timeout, this.signal);
736742
const subtests = this.skipped || this.error ? [] : this.subtests;
737743
const promise = SafePromiseAll(subtests, (subtests) => subtests.start());
738744

739745
await SafePromiseRace([promise, stopPromise]);
740746
await this[kRunHook]('after', hookArgs);
747+
748+
if (this.parent?.hooks.afterEach.length > 0) {
749+
await this.parent[kRunHook]('afterEach', hookArgs);
750+
}
751+
741752
this.pass();
742753
} catch (err) {
743754
if (isTestFailureError(err)) {
Collapse file

‎test/message/test_runner_hooks.js‎

Copy file name to clipboardExpand all lines: test/message/test_runner_hooks.js
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ describe('describe hooks', () => {
1515
'before describe hooks',
1616
'beforeEach 1', '1', 'afterEach 1',
1717
'beforeEach 2', '2', 'afterEach 2',
18+
'beforeEach nested',
1819
'before nested',
1920
'beforeEach nested 1', 'nested 1', 'afterEach nested 1',
2021
'beforeEach nested 2', 'nested 2', 'afterEach nested 2',
2122
'after nested',
23+
'afterEach nested',
2224
'after describe hooks',
2325
]);
2426
});
Collapse file

‎test/message/test_runner_test_name_pattern.js‎

Copy file name to clipboardExpand all lines: test/message/test_runner_test_name_pattern.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ test('top level test enabled', common.mustCall(async (t) => {
3434

3535
describe('top level describe enabled', () => {
3636
before(common.mustCall());
37-
beforeEach(common.mustCall(2));
38-
afterEach(common.mustCall(2));
37+
beforeEach(common.mustCall(4));
38+
afterEach(common.mustCall(4));
3939
after(common.mustCall());
4040

4141
it('nested it disabled', common.mustNotCall());

0 commit comments

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