Skip to content

Commit fb2508e

Browse files
committed
test_runner: fix nested hooks
1 parent 7b39e80 commit fb2508e

File tree

3 files changed

+20
-27
lines changed

3 files changed

+20
-27
lines changed

lib/internal/test_runner/test.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ class Test extends AsyncResource {
192192
this.testNumber = 0;
193193
this.timeout = kDefaultTimeout;
194194
this.root = this;
195+
this.hooks = {
196+
before: [],
197+
after: [],
198+
beforeEach: [],
199+
afterEach: [],
200+
};
195201
} else {
196202
const nesting = parent.parent === null ? parent.nesting :
197203
parent.nesting + 1;
@@ -204,6 +210,12 @@ class Test extends AsyncResource {
204210
this.testNumber = parent.subtests.length + 1;
205211
this.timeout = parent.timeout;
206212
this.root = parent.root;
213+
this.hooks = {
214+
before: [],
215+
after: [],
216+
beforeEach: parent.hooks.beforeEach.slice(),
217+
afterEach: parent.hooks.afterEach.slice(),
218+
};
207219
}
208220

209221
switch (typeof concurrency) {
@@ -277,12 +289,6 @@ class Test extends AsyncResource {
277289
this.pendingSubtests = [];
278290
this.readySubtests = new SafeMap();
279291
this.subtests = [];
280-
this.hooks = {
281-
before: [],
282-
after: [],
283-
beforeEach: [],
284-
afterEach: [],
285-
};
286292
this.waitingOn = 0;
287293
this.finished = false;
288294

@@ -770,11 +776,6 @@ class Suite extends Test {
770776

771777
async run() {
772778
const hookArgs = this.getRunArgs();
773-
const afterEach = runOnce(async () => {
774-
if (this.parent?.hooks.afterEach.length > 0) {
775-
await this.parent.runHook('afterEach', hookArgs);
776-
}
777-
});
778779

779780
try {
780781
this.parent.activeSubtests++;
@@ -787,10 +788,6 @@ class Suite extends Test {
787788
return;
788789
}
789790

790-
if (this.parent?.hooks.beforeEach.length > 0) {
791-
await this.parent.runHook('beforeEach', hookArgs);
792-
}
793-
794791
await this.runHook('before', hookArgs);
795792

796793
const stopPromise = stopTest(this.timeout, this.signal);
@@ -799,11 +796,9 @@ class Suite extends Test {
799796

800797
await SafePromiseRace([promise, stopPromise]);
801798
await this.runHook('after', hookArgs);
802-
await afterEach();
803799

804800
this.pass();
805801
} catch (err) {
806-
try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ }
807802
if (isTestFailureError(err)) {
808803
this.fail(err);
809804
} else {

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

Lines changed: 6 additions & 8 deletions
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

Lines changed: 2 additions & 2 deletions
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)