question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

waitForElementToBeRemoved throws error when passed an element

See original GitHub issue

What 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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
eps1loncommented, Aug 25, 2021

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.

1reaction
AriPerkkiocommented, Jul 11, 2021

The element passed to waitForElementToBeRemoved is not unmounted. React is only replacing its textContent or the text node inside it.

Refactor code to make current test pass:

  return (
    <>
-      {isLoading && "Loading..."}
+      {isLoading && (<div>Loading...</div>)}
      <div>{title}</div>
    </>
  );

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found