Skip to content

Commit

Permalink
fix: call {before,after}Each() on suites
Browse files Browse the repository at this point in the history
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 <moshe@atlow.co.il>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
(cherry picked from commit a69a30016cf3395b0bd775c1340ab6ecbac58296)
  • Loading branch information
cjihrig authored and MoLow committed Feb 2, 2023
1 parent 46dce07 commit 0bfdb77
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
13 changes: 12 additions & 1 deletion lib/internal/test_runner/test.js
@@ -1,4 +1,4 @@
// https://github.com/nodejs/node/blob/87170c3f9271da947a7b33d0696ec4cf8aab6eb6/lib/internal/test_runner/test.js
// https://github.com/nodejs/node/blob/a69a30016cf3395b0bd775c1340ab6ecbac58296/lib/internal/test_runner/test.js

'use strict'

Expand Down Expand Up @@ -738,13 +738,24 @@ class Suite extends Test {
}

const hookArgs = this.getRunArgs()

if (this.parent?.hooks.beforeEach.length > 0) {
await this.parent[kRunHook]('beforeEach', hookArgs)
}

await this[kRunHook]('before', hookArgs)

const stopPromise = stopTest(this.timeout, this.signal)
const subtests = this.skipped || this.error ? [] : this.subtests
const promise = SafePromiseAll(subtests, (subtests) => subtests.start())

await SafePromiseRace([promise, stopPromise])
await this[kRunHook]('after', hookArgs)

if (this.parent?.hooks.afterEach.length > 0) {
await this.parent[kRunHook]('afterEach', hookArgs)
}

this.pass()
} catch (err) {
if (isTestFailureError(err)) {
Expand Down
4 changes: 3 additions & 1 deletion test/message/test_runner_hooks.js
@@ -1,4 +1,4 @@
// https://github.com/nodejs/node/blob/659dc126932f986fc33c7f1c878cb2b57a1e2fac/test/message/test_runner_hooks.js
// https://github.com/nodejs/node/blob/a69a30016cf3395b0bd775c1340ab6ecbac58296/test/message/test_runner_hooks.js
// Flags: --no-warnings
'use strict'
require('../common')
Expand All @@ -16,10 +16,12 @@ describe('describe hooks', () => {
'before describe hooks',
'beforeEach 1', '1', 'afterEach 1',
'beforeEach 2', '2', 'afterEach 2',
'beforeEach nested',
'before nested',
'beforeEach nested 1', 'nested 1', 'afterEach nested 1',
'beforeEach nested 2', 'nested 2', 'afterEach nested 2',
'after nested',
'afterEach nested',
'after describe hooks'
])
})
Expand Down
6 changes: 3 additions & 3 deletions test/message/test_runner_test_name_pattern.js
@@ -1,4 +1,4 @@
// https://github.com/nodejs/node/blob/87170c3f9271da947a7b33d0696ec4cf8aab6eb6/test/message/test_runner_test_name_pattern.js
// https://github.com/nodejs/node/blob/a69a30016cf3395b0bd775c1340ab6ecbac58296/test/message/test_runner_test_name_pattern.js
// Flags: --no-warnings --test-name-pattern=enabled --test-name-pattern=/pattern/i
'use strict'
const common = require('../common')
Expand Down Expand Up @@ -35,8 +35,8 @@ test('top level test enabled', common.mustCall(async (t) => {

describe('top level describe enabled', () => {
before(common.mustCall())
beforeEach(common.mustCall(2))
afterEach(common.mustCall(2))
beforeEach(common.mustCall(4))
afterEach(common.mustCall(4))
after(common.mustCall())

it('nested it disabled', common.mustNotCall())
Expand Down

0 comments on commit 0bfdb77

Please sign in to comment.