Home > Blockchain >  Why is my test failing on an expectation wrapped in `waitFor()`?
Why is my test failing on an expectation wrapped in `waitFor()`?

Time:01-20

I have the following utility function that I use in all my tests:

async function waitForContentLoaded() {
  await waitFor(() => {
    expect(screen.queryByRole('progressbar')).not.toBeInTheDocument();
  });
}

Now 2 out of 37 tests fail with the error message expected document not to contain element, found <span {...} role="progressbar" {...} />, and a stacktrace pointing to the expectation above.

Isn't waitFor() supposed to catch these errors, and then try again until it doesn't throw?

CodePudding user response:

Here's what the documentation says:

waitFor may run the callback a number of times until the timeout is reached. Note that the number of calls is constrained by the timeout and interval options.

The default interval is 50ms. However it will run your callback immediately before starting the intervals.

The default timeout is 1000ms.

So, on the final invocation before the timeout value is exceeded, waitFor will reject with any exception which occurs in the callback you provide to it.

  •  Tags:  
  • Related