From 4e09df4dc8851a0cdd935e05052db4e2668a8b54 Mon Sep 17 00:00:00 2001 From: atlowChemi Date: Fri, 14 Apr 2023 10:12:26 +0200 Subject: [PATCH 1/3] 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 --- doc/api/test.md | 25 ++++++++ lib/internal/test_runner/test.js | 14 +++- test/fixtures/test-runner/output/hooks.js | 10 ++- .../test-runner/output/hooks.snapshot | 64 ++++++++++++++++--- 4 files changed, 102 insertions(+), 11 deletions(-) diff --git a/doc/api/test.md b/doc/api/test.md index 2f7c07aa673de1..cb03b0d323ea12 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -1498,12 +1498,37 @@ Emitted when a test starts. added: - v18.0.0 - v16.17.0 +changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/47586 + description: The `before` function was added to TestContext --> An instance of `TestContext` is passed to each test function in order to interact with the test runner. However, the `TestContext` constructor is not exposed as part of the API. +### `context.before([fn][, options])` + + + +* `fn` {Function|AsyncFunction} The hook function. The first argument + to this function is a [`TestContext`][] object. If the hook uses callbacks, + the callback function is passed as the second argument. **Default:** A no-op + function. +* `options` {Object} Configuration options for the hook. The following + properties are supported: + * `signal` {AbortSignal} Allows aborting an in-progress hook. + * `timeout` {number} A number of milliseconds the hook will fail after. + If unspecified, subtests inherit this value from their parent. + **Default:** `Infinity`. + +This function is used to create a hook running before running a suite. +before subtest of the current test. + + ### `context.beforeEach([fn][, options])` An instance of `TestContext` is passed to each test function in order to @@ -1528,7 +1528,6 @@ added: REPLACEME This function is used to create a hook running before running a suite. before subtest of the current test. - ### `context.beforeEach([fn][, options])`