Skip to content

Commit 35588cf

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 <moshe@atlow.co.il> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent ea8e72e commit 35588cf

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,
@@ -250,6 +251,7 @@ class Test extends AsyncResource {
250251
after: [],
251252
beforeEach: [],
252253
afterEach: [],
254+
ownAfterEachCount: 0,
253255
};
254256
} else {
255257
const nesting = parent.parent === null ? parent.nesting :
@@ -269,6 +271,7 @@ class Test extends AsyncResource {
269271
after: [],
270272
beforeEach: ArrayPrototypeSlice(parent.hooks.beforeEach),
271273
afterEach: ArrayPrototypeSlice(parent.hooks.afterEach),
274+
ownAfterEachCount: 0,
272275
};
273276
}
274277

@@ -502,7 +505,15 @@ class Test extends AsyncResource {
502505
}
503506
});
504507
}
505-
ArrayPrototypePush(this.hooks[name], hook);
508+
if (name === 'afterEach') {
509+
// afterEach hooks for the current test should run in the order that they
510+
// are created. However, the current test's afterEach hooks should run
511+
// prior to any ancestor afterEach hooks.
512+
ArrayPrototypeSplice(this.hooks[name], this.hooks.ownAfterEachCount, 0, hook);
513+
this.hooks.ownAfterEachCount++;
514+
} else {
515+
ArrayPrototypePush(this.hooks[name], hook);
516+
}
506517
return hook;
507518
}
508519

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)