Skip to content

Commit 008f95d

Browse files
cjihrigMoLow
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: nodejs#45028 PR-URL: nodejs#45161 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent df55f1f commit 008f95d

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

lib/internal/test_runner/test.js

+11
Original file line numberDiff line numberDiff line change
@@ -706,13 +706,24 @@ class Suite extends Test {
706706

707707

708708
const hookArgs = this.getRunArgs();
709+
710+
if (this.parent?.hooks.beforeEach.length > 0) {
711+
await this.parent[kRunHook]('beforeEach', hookArgs);
712+
}
713+
709714
await this[kRunHook]('before', hookArgs);
715+
710716
const stopPromise = stopTest(this.timeout, this.signal);
711717
const subtests = this.skipped || this.error ? [] : this.subtests;
712718
const promise = SafePromiseAll(subtests, (subtests) => subtests.start());
713719

714720
await SafePromiseRace([promise, stopPromise]);
715721
await this[kRunHook]('after', hookArgs);
722+
723+
if (this.parent?.hooks.afterEach.length > 0) {
724+
await this.parent[kRunHook]('afterEach', hookArgs);
725+
}
726+
716727
this.pass();
717728
} catch (err) {
718729
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
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Flags: --no-warnings --test-name-pattern=enabled --test-name-pattern=/pattern/i
2+
'use strict';
3+
const common = require('../common');
4+
const {
5+
after,
6+
afterEach,
7+
before,
8+
beforeEach,
9+
describe,
10+
it,
11+
test,
12+
} = require('node:test');
13+
14+
test('top level test disabled', common.mustNotCall());
15+
test('top level skipped test disabled', { skip: true }, common.mustNotCall());
16+
test('top level skipped test enabled', { skip: true }, common.mustNotCall());
17+
it('top level it enabled', common.mustCall());
18+
it('top level it disabled', common.mustNotCall());
19+
it.skip('top level skipped it disabled', common.mustNotCall());
20+
it.skip('top level skipped it enabled', common.mustNotCall());
21+
describe('top level describe disabled', common.mustNotCall());
22+
describe.skip('top level skipped describe disabled', common.mustNotCall());
23+
describe.skip('top level skipped describe enabled', common.mustNotCall());
24+
test('top level runs because name includes PaTtErN', common.mustCall());
25+
26+
test('top level test enabled', common.mustCall(async (t) => {
27+
t.beforeEach(common.mustCall());
28+
t.afterEach(common.mustCall());
29+
await t.test(
30+
'nested test runs because name includes PATTERN',
31+
common.mustCall()
32+
);
33+
}));
34+
35+
describe('top level describe enabled', () => {
36+
before(common.mustCall());
37+
beforeEach(common.mustCall(4));
38+
afterEach(common.mustCall(4));
39+
after(common.mustCall());
40+
41+
it('nested it disabled', common.mustNotCall());
42+
it('nested it enabled', common.mustCall());
43+
describe('nested describe disabled', common.mustNotCall());
44+
describe('nested describe enabled', common.mustCall(() => {
45+
it('is enabled', common.mustCall());
46+
}));
47+
});

0 commit comments

Comments
 (0)