From bbc5623d8acf642d4aba929faa283810e405f8df Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Wed, 15 Jan 2025 21:32:37 -0500 Subject: [PATCH] test_runner: add TestContext.prototype.waitFor() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds a waitFor() method to the TestContext class in the test runner. As the name implies, this method allows tests to more easily wait for things to happen. PR-URL: https://github.com/nodejs/node/pull/56595 Reviewed-By: Pietro Marchini Reviewed-By: Matteo Collina Reviewed-By: Chemi Atlow Reviewed-By: Michaƫl Zasso --- doc/api/test.md | 21 +++++ lib/internal/test_runner/test.js | 62 ++++++++++++- test/parallel/test-runner-wait-for.js | 124 ++++++++++++++++++++++++++ 3 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-runner-wait-for.js diff --git a/doc/api/test.md b/doc/api/test.md index 3b0c040d852334..68b4a0f8a6a969 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -3604,6 +3604,27 @@ test('top level test', async (t) => { }); ``` +### `context.waitFor(condition[, options])` + + + +* `condition` {Function|AsyncFunction} An assertion function that is invoked + periodically until it completes successfully or the defined polling timeout + elapses. Successful completion is defined as not throwing or rejecting. This + function does not accept any arguments, and is allowed to return any value. +* `options` {Object} An optional configuration object for the polling operation. + The following properties are supported: + * `interval` {number} The number of milliseconds to wait after an unsuccessful + invocation of `condition` before trying again. **Default:** `50`. + * `timeout` {number} The poll timeout in milliseconds. If `condition` has not + succeeded by the time this elapses, an error occurs. **Default:** `1000`. +* Returns: {Promise} Fulfilled with the value returned by `condition`. + +This method polls a `condition` function until that function either returns +successfully or the operation times out. + ## Class: `SuiteContext`