diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index c1203c91177f437..1c47ee0d83ffce1 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -414,6 +414,9 @@ class Test extends AsyncResource { validateOneOf(name, 'hook name', kHookNames); // eslint-disable-next-line no-use-before-define const hook = new TestHook(fn, options); + if (name === 'before') { + hook.run = runOnce(hook.run); + } ArrayPrototypePush(this.hooks[name], hook); return hook; } @@ -525,6 +528,9 @@ class Test extends AsyncResource { if (this.parent?.hooks.beforeEach.length > 0) { await this.parent.runHook('beforeEach', { args, ctx }); } + if (this.parent?.hooks.before.length > 0) { + await this.parent.runHook('before', { args, ctx }); + } const stopPromise = stopTest(this.timeout, this.signal); const runArgs = ArrayPrototypeSlice(args); ArrayPrototypeUnshift(runArgs, this.fn, ctx); @@ -561,7 +567,7 @@ class Test extends AsyncResource { this.pass(); } catch (err) { try { await after(); } catch { /* Ignore error. */ } - try { await afterEach(); } catch { /* test is already failing, let's the error */ } + try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ } if (isTestFailureError(err)) { if (err.failureType === kTestTimeoutFailure) { this.#cancel(err); @@ -793,7 +799,7 @@ class Suite extends Test { this.pass(); } catch (err) { - try { await afterEach(); } catch { /* test is already failing, let's the error */ } + try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ } if (isTestFailureError(err)) { this.fail(err); } else {