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: https://github.com/nodejs/node/issues/45028
PR-URL: https://github.com/nodejs/node/pull/45161
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Colin Ihrig
2022-10-27 04:27:35 -04:00
committed by GitHub
parent aaed438165
commit a69a30016c
3 changed files with 15 additions and 2 deletions

View File

@@ -731,13 +731,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)) {

View File

@@ -15,10 +15,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',
]);
});

View File

@@ -34,8 +34,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());