为什么我的测试因包含在 waitFor() 中的期望而失败?

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

我在所有测试中都使用了以下实用函数:

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

现在 37 个测试中有 2 个失败并显示错误消息 expected document not to contain element, found <span {...} role="progressbar" {...} />,并且堆栈跟踪指向上述预期。

难道 waitFor() 不应该捕获这些错误,然后重试直到它不抛出吗?

the documentation 是这样说的:

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.

因此,在超过 timeout 值之前的最后一次调用中,waitFor 将拒绝您提供给它的回调中发生的任何异常。