From 4165dcddf046b7f4c27bfcc5f88d7852d71bf84e Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 8 Dec 2022 17:21:22 -0500 Subject: [PATCH] test_runner: add t.after() hook This commit adds an after() hook to the TestContext class. This hook can be used to clean up after a test finishes. PR-URL: https://github.com/nodejs/node/pull/45792 Reviewed-By: Moshe Atlow Reviewed-By: Matteo Collina --- doc/api/test.md | 27 +++++++++++++++++++++++++++ lib/internal/test_runner/test.js | 5 +++++ test/message/test_runner_hooks.js | 8 +++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/doc/api/test.md b/doc/api/test.md index c8f8dfd0f62c2e..169c657994b056 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -1133,6 +1133,33 @@ test('top level test', async (t) => { }); ``` +### `context.after([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 that runs after the current test +finishes. + +```js +test('top level test', async (t) => { + t.after((t) => t.diagnostic(`finished running ${t.name}`)); + assert.ok('some relevant assertion here'); +}); +``` + ### `context.afterEach([fn][, options])`