Skip to content

Commit 0bfdb77

Browse files
cjihrigMoLow
authored andcommitted
fix: 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/node#45028 PR-URL: nodejs/node#45161 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: James M Snell <[email protected]> (cherry picked from commit a69a30016cf3395b0bd775c1340ab6ecbac58296)
1 parent 46dce07 commit 0bfdb77

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

lib/internal/test_runner/test.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/87170c3f9271da947a7b33d0696ec4cf8aab6eb6/lib/internal/test_runner/test.js
1+
// https://github.com/nodejs/node/blob/a69a30016cf3395b0bd775c1340ab6ecbac58296/lib/internal/test_runner/test.js
22

33
'use strict'
44

@@ -738,13 +738,24 @@ class Suite extends Test {
738738
}
739739

740740
const hookArgs = this.getRunArgs()
741+
742+
if (this.parent?.hooks.beforeEach.length > 0) {
743+
await this.parent[kRunHook]('beforeEach', hookArgs)
744+
}
745+
741746
await this[kRunHook]('before', hookArgs)
747+
742748
const stopPromise = stopTest(this.timeout, this.signal)
743749
const subtests = this.skipped || this.error ? [] : this.subtests
744750
const promise = SafePromiseAll(subtests, (subtests) => subtests.start())
745751

746752
await SafePromiseRace([promise, stopPromise])
747753
await this[kRunHook]('after', hookArgs)
754+
755+
if (this.parent?.hooks.afterEach.length > 0) {
756+
await this.parent[kRunHook]('afterEach', hookArgs)
757+
}
758+
748759
this.pass()
749760
} catch (err) {
750761
if (isTestFailureError(err)) {

test/message/test_runner_hooks.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/659dc126932f986fc33c7f1c878cb2b57a1e2fac/test/message/test_runner_hooks.js
1+
// https://github.com/nodejs/node/blob/a69a30016cf3395b0bd775c1340ab6ecbac58296/test/message/test_runner_hooks.js
22
// Flags: --no-warnings
33
'use strict'
44
require('../common')
@@ -16,10 +16,12 @@ describe('describe hooks', () => {
1616
'before describe hooks',
1717
'beforeEach 1', '1', 'afterEach 1',
1818
'beforeEach 2', '2', 'afterEach 2',
19+
'beforeEach nested',
1920
'before nested',
2021
'beforeEach nested 1', 'nested 1', 'afterEach nested 1',
2122
'beforeEach nested 2', 'nested 2', 'afterEach nested 2',
2223
'after nested',
24+
'afterEach nested',
2325
'after describe hooks'
2426
])
2527
})

test/message/test_runner_test_name_pattern.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/87170c3f9271da947a7b33d0696ec4cf8aab6eb6/test/message/test_runner_test_name_pattern.js
1+
// https://github.com/nodejs/node/blob/a69a30016cf3395b0bd775c1340ab6ecbac58296/test/message/test_runner_test_name_pattern.js
22
// Flags: --no-warnings --test-name-pattern=enabled --test-name-pattern=/pattern/i
33
'use strict'
44
const common = require('../common')
@@ -35,8 +35,8 @@ test('top level test enabled', common.mustCall(async (t) => {
3535

3636
describe('top level describe enabled', () => {
3737
before(common.mustCall())
38-
beforeEach(common.mustCall(2))
39-
afterEach(common.mustCall(2))
38+
beforeEach(common.mustCall(4))
39+
afterEach(common.mustCall(4))
4040
after(common.mustCall())
4141

4242
it('nested it disabled', common.mustNotCall())

0 commit comments

Comments
 (0)