Skip to content

Commit 5a9b71a

Browse files
MoLowtargos
authored andcommitted
test_runner: fix nested hooks
PR-URL: #47648 Fixes: #47643 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 4cef988 commit 5a9b71a

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

lib/internal/test_runner/test.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ class Test extends AsyncResource {
192192
this.testNumber = 0;
193193
this.timeout = kDefaultTimeout;
194194
this.root = this;
195+
this.hooks = {
196+
__proto__: null,
197+
before: [],
198+
after: [],
199+
beforeEach: [],
200+
afterEach: [],
201+
};
195202
} else {
196203
const nesting = parent.parent === null ? parent.nesting :
197204
parent.nesting + 1;
@@ -204,6 +211,13 @@ class Test extends AsyncResource {
204211
this.testNumber = parent.subtests.length + 1;
205212
this.timeout = parent.timeout;
206213
this.root = parent.root;
214+
this.hooks = {
215+
__proto__: null,
216+
before: [],
217+
after: [],
218+
beforeEach: ArrayPrototypeSlice(parent.hooks.beforeEach),
219+
afterEach: ArrayPrototypeSlice(parent.hooks.afterEach),
220+
};
207221
}
208222

209223
switch (typeof concurrency) {
@@ -277,12 +291,6 @@ class Test extends AsyncResource {
277291
this.pendingSubtests = [];
278292
this.readySubtests = new SafeMap();
279293
this.subtests = [];
280-
this.hooks = {
281-
before: [],
282-
after: [],
283-
beforeEach: [],
284-
afterEach: [],
285-
};
286294
this.waitingOn = 0;
287295
this.finished = false;
288296

@@ -772,11 +780,6 @@ class Suite extends Test {
772780

773781
async run() {
774782
const hookArgs = this.getRunArgs();
775-
const afterEach = runOnce(async () => {
776-
if (this.parent?.hooks.afterEach.length > 0) {
777-
await this.parent.runHook('afterEach', hookArgs);
778-
}
779-
});
780783

781784
try {
782785
this.parent.activeSubtests++;
@@ -789,10 +792,6 @@ class Suite extends Test {
789792
return;
790793
}
791794

792-
if (this.parent?.hooks.beforeEach.length > 0) {
793-
await this.parent.runHook('beforeEach', hookArgs);
794-
}
795-
796795
await this.runHook('before', hookArgs);
797796

798797
const stopPromise = stopTest(this.timeout, this.signal);
@@ -801,11 +800,9 @@ class Suite extends Test {
801800

802801
await SafePromiseRace([promise, stopPromise]);
803802
await this.runHook('after', hookArgs);
804-
await afterEach();
805803

806804
this.pass();
807805
} catch (err) {
808-
try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ }
809806
if (isTestFailureError(err)) {
810807
this.fail(err);
811808
} else {

test/fixtures/test-runner/output/hooks.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ describe('describe hooks', () => {
1717
'before describe hooks',
1818
'beforeEach 1', '1', 'afterEach 1',
1919
'beforeEach 2', '2', 'afterEach 2',
20-
'beforeEach nested',
2120
'before nested',
22-
'beforeEach nested 1', 'nested 1', 'afterEach nested 1',
23-
'beforeEach nested 2', 'nested 2', 'afterEach nested 2',
21+
'beforeEach nested 1', '+beforeEach nested 1', 'nested 1', 'afterEach nested 1', '+afterEach nested 1',
22+
'beforeEach nested 2', '+beforeEach nested 2', 'nested 2', 'afterEach nested 2', '+afterEach nested 2',
2423
'after nested',
25-
'afterEach nested',
2624
'after describe hooks',
2725
]);
2826
});
@@ -44,10 +42,10 @@ describe('describe hooks', () => {
4442
testArr.push('after ' + this.name);
4543
});
4644
beforeEach(function() {
47-
testArr.push('beforeEach ' + this.name);
45+
testArr.push('+beforeEach ' + this.name);
4846
});
4947
afterEach(function() {
50-
testArr.push('afterEach ' + this.name);
48+
testArr.push('+afterEach ' + this.name);
5149
});
5250
it('nested 1', () => testArr.push('nested 1'));
5351
test('nested 2', () => testArr.push('nested 2'));
@@ -111,8 +109,8 @@ test('test hooks', async (t) => {
111109
'beforeEach 1', 'before test hooks', '1', 'afterEach 1',
112110
'beforeEach 2', '2', 'afterEach 2',
113111
'beforeEach nested',
114-
'nested beforeEach nested 1', 'nested1', 'nested afterEach nested 1',
115-
'nested beforeEach nested 2', 'nested 2', 'nested afterEach nested 2',
112+
'beforeEach nested 1', 'nested beforeEach nested 1', 'nested1', 'afterEach nested 1', 'nested afterEach nested 1',
113+
'beforeEach nested 2', 'nested beforeEach nested 2', 'nested 2', 'afterEach nested 2', 'nested afterEach nested 2',
116114
'afterEach nested',
117115
]);
118116
});

test/fixtures/test-runner/output/name_pattern.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ test('top level test enabled', common.mustCall(async (t) => {
3434

3535
describe('top level describe enabled', () => {
3636
before(common.mustCall());
37-
beforeEach(common.mustCall(4));
38-
afterEach(common.mustCall(4));
37+
beforeEach(common.mustCall(3));
38+
afterEach(common.mustCall(3));
3939
after(common.mustCall());
4040

4141
it('nested it disabled', common.mustNotCall());

0 commit comments

Comments
 (0)