Skip to content

Commit afa8291

Browse 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 <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 916e876 commit afa8291

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

lib/internal/test_runner/test.js

+11
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)) {

test/message/test_runner_hooks.js

+2
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
});

test/message/test_runner_test_name_pattern.js

+2-2
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)