Skip to content

Commit 3484929

Browse files
committed
test_runner: run global before hook after bootstrap
This commit updates top level 'before' hook logic so that the hooks do not run before the test runner finishes bootstrapping. This is necessary for running multiple test files in the same process.
1 parent 2d7018c commit 3484929

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

lib/internal/test_runner/harness.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ function setup(root) {
163163
const exitHandler = async () => {
164164
if (root.subtests.length === 0 && (root.hooks.before.length > 0 || root.hooks.after.length > 0)) {
165165
// Run global before/after hooks in case there are no tests
166+
await root.runHook('before', root.getRunArgs());
166167
await root.run();
167168
}
168169
root.postRun(new ERR_TEST_FAILURE(

lib/internal/test_runner/test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,10 @@ class Test extends AsyncResource {
682682
if (name === 'before' || name === 'after') {
683683
hook.run = runOnce(hook.run, kRunOnceOptions);
684684
}
685-
if (name === 'before' && this.startTime !== null) {
686-
// Test has already started, run the hook immediately
685+
// If the test has already started, run the hook immediately. Do not run
686+
// top level hooks immediately though so that we have a chance to load all
687+
// of the tests.
688+
if (name === 'before' && this.startTime !== null && this.parent !== null) {
687689
PromisePrototypeThen(hook.run(this.getRunArgs()), () => {
688690
if (hook.error != null) {
689691
this.fail(hook.error);

test/fixtures/test-runner/output/global-hooks-with-no-tests.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
before
21
TAP version 13
2+
before
33
after
44
1..0
55
# tests 0

0 commit comments

Comments
 (0)