Skip to content

Commit 7c02486

Browse files
authored
test_runner: run afterEach hooks in correct order
This commit updates the test runner afterEach hook so that the current test's afterEach hooks run before any ancestor afterEach hooks. Fixes: #51671 PR-URL: #52239 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent dde0cff commit 7c02486

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

lib/internal/test_runner/test.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
ArrayPrototypeShift,
77
ArrayPrototypeSlice,
88
ArrayPrototypeSome,
9+
ArrayPrototypeSplice,
910
ArrayPrototypeUnshift,
1011
FunctionPrototype,
1112
MathMax,
@@ -275,6 +276,7 @@ class Test extends AsyncResource {
275276
after: [],
276277
beforeEach: [],
277278
afterEach: [],
279+
ownAfterEachCount: 0,
278280
};
279281
} else {
280282
const nesting = parent.parent === null ? parent.nesting :
@@ -294,6 +296,7 @@ class Test extends AsyncResource {
294296
after: [],
295297
beforeEach: ArrayPrototypeSlice(parent.hooks.beforeEach),
296298
afterEach: ArrayPrototypeSlice(parent.hooks.afterEach),
299+
ownAfterEachCount: 0,
297300
};
298301

299302
if ((testNamePatterns !== null && !this.matchesTestNamePatterns()) ||
@@ -559,7 +562,15 @@ class Test extends AsyncResource {
559562
}
560563
});
561564
}
562-
ArrayPrototypePush(this.hooks[name], hook);
565+
if (name === 'afterEach') {
566+
// afterEach hooks for the current test should run in the order that they
567+
// are created. However, the current test's afterEach hooks should run
568+
// prior to any ancestor afterEach hooks.
569+
ArrayPrototypeSplice(this.hooks[name], this.hooks.ownAfterEachCount, 0, hook);
570+
this.hooks.ownAfterEachCount++;
571+
} else {
572+
ArrayPrototypePush(this.hooks[name], hook);
573+
}
563574
return hook;
564575
}
565576

test/fixtures/test-runner/output/hooks-with-no-global-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ after(() => {
2626
'describe beforeEach',
2727
'describe nested beforeEach',
2828
'describe nested it 1',
29-
'describe afterEach',
3029
'describe nested afterEach',
30+
'describe afterEach',
3131

3232
'describe beforeEach',
3333
'describe nested beforeEach',
3434
'describe nested test 2',
35-
'describe afterEach',
3635
'describe nested afterEach',
36+
'describe afterEach',
3737

3838
'describe nested after',
3939
'describe after',

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ describe('describe hooks', () => {
1818
'beforeEach 1', '1', 'afterEach 1',
1919
'beforeEach 2', '2', 'afterEach 2',
2020
'before nested',
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',
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',
2323
'after nested',
2424
'after describe hooks',
2525
]);
@@ -139,8 +139,8 @@ test('test hooks', async (t) => {
139139
'beforeEach 2', '2', 'afterEach 2',
140140
'beforeEach nested',
141141
'nested before nested',
142-
'beforeEach nested 1', 'nested beforeEach nested 1', 'nested1', 'afterEach nested 1', 'nested afterEach nested 1',
143-
'beforeEach nested 2', 'nested beforeEach nested 2', 'nested 2', 'afterEach nested 2', 'nested afterEach nested 2',
142+
'beforeEach nested 1', 'nested beforeEach nested 1', 'nested1', 'nested afterEach nested 1', 'afterEach nested 1',
143+
'beforeEach nested 2', 'nested beforeEach nested 2', 'nested 2', 'nested afterEach nested 2', 'afterEach nested 2',
144144
'afterEach nested',
145145
'nested after nested',
146146
'after test hooks',

0 commit comments

Comments
 (0)