From 0bfdb77aeeb913aac43c5cac2c5333d42fde558b Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Thu, 27 Oct 2022 04:27:35 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/45028 PR-URL: https://github.com/nodejs/node/pull/45161 Reviewed-By: Moshe Atlow Reviewed-By: Yagiz Nizipli Reviewed-By: James M Snell (cherry picked from commit a69a30016cf3395b0bd775c1340ab6ecbac58296) --- lib/internal/test_runner/test.js | 13 ++++++++++++- test/message/test_runner_hooks.js | 4 +++- test/message/test_runner_test_name_pattern.js | 6 +++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index bf09690..ce1ae08 100644 --- a/lib/internal/test_runner/test.js +++ b/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' @@ -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)) { diff --git a/test/message/test_runner_hooks.js b/test/message/test_runner_hooks.js index 836ef0e..e74048f 100644 --- a/test/message/test_runner_hooks.js +++ b/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') @@ -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' ]) }) diff --git a/test/message/test_runner_test_name_pattern.js b/test/message/test_runner_test_name_pattern.js index f0b44aa..5f7c25b 100644 --- a/test/message/test_runner_test_name_pattern.js +++ b/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') @@ -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())