From eb8dfe1fae6b6d7aaa6746e3f01c3a7bd6831641 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 --- doc/api/test.md | 26 ++++++++ lib/internal/test_runner/test.js | 14 +++- test/fixtures/test-runner/output/hooks.js | 10 ++- .../test-runner/output/hooks.snapshot | 64 ++++++++++++++++--- 4 files changed, 103 insertions(+), 11 deletions(-) diff --git a/doc/api/test.md b/doc/api/test.md index 2f7c07aa673de1..8a1af73ef886f1 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -1498,12 +1498,38 @@ 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])`