waitForElementToBeRemoved throws error when passed an element
See original GitHub issueWhat you did:
Wrote a test with await waitForElementToBeRemoved(screen.queryByText("Loading..."));
What happened:
Got a failing test Timed out in waitForElementToBeRemoved.
Reproduction:
https://codesandbox.io/s/testing-userevent-the-proper-way-forked-mx356?file=/src/App.test.js
Problem description:
As far as I can see, waitForElementToBeRemoved
accepts either a function or an element. I’d expect those to work identically.
However, await waitForElementToBeRemoved(() => screen.queryByText("Loading..."));
works, while await waitForElementToBeRemoved(screen.queryByText("Loading..."));
does not work.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
waitForElementToBeRemoved is timing out when passed an ...
When I pass in a function, it works. My understanding from this feature was that it should be able to take an element:...
Read more >Async Methods - Testing Library
The waitForElementToBeRemoved function is a small wrapper around the waitFor utility. The first argument must be an element, array of elements, ...
Read more >Testing Library - Queries - DEV Community
The waitFor async helper function retries until the wrapped function stops throwing an error. This can be used to assert that an element...
Read more >Timed out in waitForElementToBeRemoved error in RTL
The above test is getting passed but getting the classic act error. When testing, code that causes React state updates should be wrapped ......
Read more >waitForElementToBeRemoved - rtl.dom.async library - Pub.dev
API docs for the waitForElementToBeRemoved function from the ... throw TestingLibraryElementError( 'The element returned from the callback was not present ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Closing since this is expected behavior. The element isn’t removed but it’s original query is no longer matching.
You probably want to
waitFor
screen.queryByText("Loading...")
no longer returning an element.The element passed to
waitForElementToBeRemoved
is not unmounted. React is only replacing itstextContent
or the text node inside it.Refactor code to make current test pass:
Now the element will actually unmount.
When passing a callback to
waitForElementToBeRemoved
it is called repeatedly until it throws. That’s why the other solution is currently passing the test.