From e97eefa53877ea469f1795060a212d6ccfd57171 Mon Sep 17 00:00:00 2001 From: Chemi Atlow Date: Tue, 18 Apr 2023 23:59:49 +0300 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 PR-URL: https://github.com/nodejs/node/pull/47586 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Moshe Atlow --- doc/api/test.md | 24 +++++++ lib/internal/test_runner/test.js | 14 +++- test/fixtures/test-runner/output/hooks.js | 14 +++- .../test-runner/output/hooks.snapshot | 71 +++++++++++++++---- 4 files changed, 107 insertions(+), 16 deletions(-) diff --git a/doc/api/test.md b/doc/api/test.md index 367b9d9dfa841c..0cdf37e42c4cec 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -1429,12 +1429,36 @@ Emitted when a test starts. 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 +subtest of the current test. + ### `context.beforeEach([fn][, options])`