Skip to content

Commit 268dc70

Browse files
cjihrigmarco-ippolito
authored andcommitted
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 <[email protected]> Reviewed-By: Chemi Atlow <[email protected]>
1 parent 685a0ec commit 268dc70

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

lib/internal/test_runner/test.js

Lines changed: 12 additions & 1 deletion
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,
@@ -264,6 +265,7 @@ class Test extends AsyncResource {
264265
after: [],
265266
beforeEach: [],
266267
afterEach: [],
268+
ownAfterEachCount: 0,
267269
};
268270
} else {
269271
const nesting = parent.parent === null ? parent.nesting :
@@ -283,6 +285,7 @@ class Test extends AsyncResource {
283285
after: [],
284286
beforeEach: ArrayPrototypeSlice(parent.hooks.beforeEach),
285287
afterEach: ArrayPrototypeSlice(parent.hooks.afterEach),
288+
ownAfterEachCount: 0,
286289
};
287290
}
288291

@@ -527,7 +530,15 @@ class Test extends AsyncResource {
527530
}
528531
});
529532
}
530-
ArrayPrototypePush(this.hooks[name], hook);
533+
if (name === 'afterEach') {
534+
// afterEach hooks for the current test should run in the order that they
535+
// are created. However, the current test's afterEach hooks should run
536+
// prior to any ancestor afterEach hooks.
537+
ArrayPrototypeSplice(this.hooks[name], this.hooks.ownAfterEachCount, 0, hook);
538+
this.hooks.ownAfterEachCount++;
539+
} else {
540+
ArrayPrototypePush(this.hooks[name], hook);
541+
}
531542
return hook;
532543
}
533544

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

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

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