From 2ec4ef7c24a0b1c63ac54a9a042790eee4a01040 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 2a7cfecf8f1d68..4b33b37e11fbf3 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -1498,12 +1498,36 @@ 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 +subtest of the current test. + ### `context.beforeEach([fn][, options])`