【问题标题】:Why is my test failing on an expectation wrapped in `waitFor()`?为什么我的测试对包含在 `waitFor()` 中的期望失败?
【发布时间】:2022-01-19 22:25:29
【问题描述】:

我在所有测试中都使用了以下实用功能:

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

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

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

【问题讨论】:

标签: reactjs testing jestjs react-testing-library


【解决方案1】:

the documentation 是这样说的:

waitFor 可能会多次运行回调,直到达到超时。请注意,调用次数受timeoutinterval 选项的限制。

默认的interval50ms。但是,它会在开始间隔之前立即运行您的回调。

默认timeout1000ms

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

【讨论】:

  • 啊,这很有意义。谢谢!
猜你喜欢
  • 2016-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多