Skip to content

Commit

Permalink
test_runner: run global before hook after bootstrap
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cjihrig committed Jul 18, 2024
1 parent 2d7018c commit 3484929
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ function setup(root) {
const exitHandler = async () => {
if (root.subtests.length === 0 && (root.hooks.before.length > 0 || root.hooks.after.length > 0)) {
// Run global before/after hooks in case there are no tests
await root.runHook('before', root.getRunArgs());
await root.run();
}
root.postRun(new ERR_TEST_FAILURE(
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,10 @@ class Test extends AsyncResource {
if (name === 'before' || name === 'after') {
hook.run = runOnce(hook.run, kRunOnceOptions);
}
if (name === 'before' && this.startTime !== null) {
// Test has already started, run the hook immediately
// If the test has already started, run the hook immediately. Do not run
// top level hooks immediately though so that we have a chance to load all
// of the tests.
if (name === 'before' && this.startTime !== null && this.parent !== null) {
PromisePrototypeThen(hook.run(this.getRunArgs()), () => {
if (hook.error != null) {
this.fail(hook.error);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
before
TAP version 13
before
after
1..0
# tests 0
Expand Down

0 comments on commit 3484929

Please sign in to comment.