Skip to content

Commit 858b583

Browse files
cjihrigRafaelGSS
authored andcommitted
test_runner: defer inheriting hooks until run()
This commit updates the way the test runner computes inherited hooks. Instead of computing them when the Test/Suite is constructed, they are now computed just prior to running the Test/Suite. The reason is because when multiple test files are run in the same process, it is possible for the inherited hooks to change as more files are loaded. PR-URL: #53927 Fixes: #51548 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 45b0250 commit 858b583

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

lib/internal/test_runner/test.js

+25-17
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,6 @@ class Test extends AsyncResource {
408408
this.childNumber = 0;
409409
this.timeout = kDefaultTimeout;
410410
this.entryFile = entryFile;
411-
this.hooks = {
412-
__proto__: null,
413-
before: [],
414-
after: [],
415-
beforeEach: [],
416-
afterEach: [],
417-
ownAfterEachCount: 0,
418-
};
419411
} else {
420412
const nesting = parent.parent === null ? parent.nesting :
421413
parent.nesting + 1;
@@ -431,14 +423,6 @@ class Test extends AsyncResource {
431423
this.childNumber = parent.subtests.length + 1;
432424
this.timeout = parent.timeout;
433425
this.entryFile = parent.entryFile;
434-
this.hooks = {
435-
__proto__: null,
436-
before: [],
437-
after: [],
438-
beforeEach: ArrayPrototypeSlice(parent.hooks.beforeEach),
439-
afterEach: ArrayPrototypeSlice(parent.hooks.afterEach),
440-
ownAfterEachCount: 0,
441-
};
442426

443427
if (this.willBeFiltered()) {
444428
this.filtered = true;
@@ -514,6 +498,14 @@ class Test extends AsyncResource {
514498
this.subtests = [];
515499
this.waitingOn = 0;
516500
this.finished = false;
501+
this.hooks = {
502+
__proto__: null,
503+
before: [],
504+
after: [],
505+
beforeEach: [],
506+
afterEach: [],
507+
ownAfterEachCount: 0,
508+
};
517509

518510
if (!this.config.only && (only || this.parent?.runOnlySubtests)) {
519511
const warning =
@@ -691,6 +683,21 @@ class Test extends AsyncResource {
691683
this.abortController.abort();
692684
}
693685

686+
computeInheritedHooks() {
687+
if (this.parent.hooks.beforeEach.length > 0) {
688+
ArrayPrototypeUnshift(
689+
this.hooks.beforeEach,
690+
...ArrayPrototypeSlice(this.parent.hooks.beforeEach),
691+
);
692+
}
693+
694+
if (this.parent.hooks.afterEach.length > 0) {
695+
ArrayPrototypePushApply(
696+
this.hooks.afterEach, ArrayPrototypeSlice(this.parent.hooks.afterEach),
697+
);
698+
}
699+
}
700+
694701
createHook(name, fn, options) {
695702
validateOneOf(name, 'hook name', kHookNames);
696703
// eslint-disable-next-line no-use-before-define
@@ -715,7 +722,6 @@ class Test extends AsyncResource {
715722
} else {
716723
ArrayPrototypePush(this.hooks[name], hook);
717724
}
718-
return hook;
719725
}
720726

721727
fail(err) {
@@ -817,6 +823,7 @@ class Test extends AsyncResource {
817823
async run() {
818824
if (this.parent !== null) {
819825
this.parent.activeSubtests++;
826+
this.computeInheritedHooks();
820827
}
821828
this.startTime ??= hrtime();
822829

@@ -1211,6 +1218,7 @@ class Suite extends Test {
12111218
}
12121219

12131220
async run() {
1221+
this.computeInheritedHooks();
12141222
const hookArgs = this.getRunArgs();
12151223

12161224
let stopPromise;

0 commit comments

Comments
 (0)