Docs (4.0.0)
class TestCase method

pollAsync

Runs a function at the specified interval (in ms).

pollAsync(poll: () => true | any, interval?: number, timeout?: number, onTimeout?: () => Error | void): Promise<void>

Summary

This method calls a poll function at a specific interval (in milliseconds), until it returns true. When it does, the promise returned by this method is resolved, and the test case may continue.

If a timeout is provided (also in milliseconds), then the returned promise is rejected after the specified amount of time and the poll function will no longer be called — if the function didn’t already return true or threw an error, and if the test hasn’t yet failed otherwise. The returned promise is rejected with a generic timeout error, or the result of the onTimeout function if provided.

  • The provided function runs synchronously, promise return values aren’t awaited.
  • If the provided function throws an error, the error is passed on: the promise returned by this method is rejected.
  • If the test itself fails in the meantime, the provided function will no longer be called.

Note
This method is asynchronous, and must be awaited (otherwise any running test will fail automatically).

Parameters

  • poll — The function to call at the specified interval
  • interval — The poll interval, in milliseconds
  • timeout — The timeout, in milliseconds, after which the returned promise is rejected
  • onTimeout — A function that’s called just before the promise is rejected on timeout; may return an error which is used when rejecting the promise

Return value

A promise (void) that’s resolved when polling stops, or rejected if the function throws an error or a timeout occurs

Example

describe("My scope", () => {
  test("Do something", async (t) => {
    // ... start something that goes on for a while
    await t.pollAsync(() => {
      if (something.isDone()) return true;
      if (something.hasFailed()) throw Error("Oops");
    }, 100);
  });
});

Related