首页 > 解决方案 > 传递元素时,waitForElementToBeRemoved 超时

问题描述

我正在尝试waitForElementToBeRemoved仅使用一个元素,但 Jest 正在超时。当我传入一个函数时,它可以工作。

我对这个特性的理解是它应该能够接受一个元素:https ://github.com/testing-library/dom-testing-library/pull/460

我验证了我的应用程序正在使用@testing-library/dom@7.8.0。

这是代码:

// doesn't work
await waitForElementToBeRemoved(screen.getByText("Loading..."));

// works
await waitForElementToBeRemoved(() => screen.getByText("Loading..."));

知道我做错了什么吗?

标签: react-testing-library

解决方案


除了我之前的回答,我建议使用queryByText

await waitForElementToBeRemoved(screen.queryByText("Loading..."));

而不是getByText

await waitForElementToBeRemoved(screen.getByText("Loading..."));

请参阅React 测试库的常见错误


推荐阅读