From 81dcac9a024ce5a85c08ecebbbcb16223d0ed9a7 Mon Sep 17 00:00:00 2001 From: atlowChemi Date: Fri, 14 Apr 2023 10:12:26 +0200 Subject: [PATCH] test_runner: execute before hook on test Execute the before hook for Test as well, and execute it on root before executing any tests. Fixes: https://github.com/nodejs/node/issues/47518 --- lib/internal/test_runner/test.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index c1203c91177f43..1c47ee0d83ffce 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 {