waitFor + getByRole causing severe delays
See original GitHub issue@testing-library/dom
version: 7.26.6node
version: 14.3.0
Relevant code or config:
it("Causes a big delay", async () => {
// This fires a fetch request that is mocked using MSW (so it should be near instant)
userEvent.click(screen.getByRole("button", { name: "Search"}));
await waitFor(() => {
//super fast call
//screen.getByText("some_link_text");
// causes massive delays
screen.getByRole("link", { name: "some_link" });
});
});
What you did:
Running this test causes the fetch request promise to resolve a lot longer than usual (around 1.5 - 2s). I initially investigated this by wrapping console.time
around the request.
I also had an additional test which looks very similar to the above, but instead of using getByRole
it was using getByText
to search for some text. That test took a fraction of the time:
What happened:
After changing getByRole
to getByText
or inserting a getByText
query before the getByRole
it resolved much faster. My working theory is that getByRole
is so expensive that it was monopolising the waitFor
loop and takes a long time to relinquish control to the pending promise that needs to resolve.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:25
- Comments:58 (30 by maintainers)
Top Results From Across the Web
Async Methods - Testing Library
findBy methods are a combination of getBy queries and waitFor . They accept the waitFor options as the last argument (e.g. await screen....
Read more >Uživatel Cory House na Twitteru: „I rarely use a div/span/p. I ...
Edit: I meant to say “getByRole” in the first tweet. ... waitFor + getByRole causing severe delays · Issue #820 · testing-library/dom-testing-library.
Read more >Want to increase your quality and reduce your deployment ...
Backend services have their own deployments and performance testing in different ... waitFor + getByRole causing severe delays · Issue #820 ...
Read more >React hooks (v17.0.3) and Redux handbook using TypeScript ...
React Hooks #. React hooks exist because there were severe limitations in versions before React 16 on what function components could do. And...
Read more >React Testing Library's waitFor not working - Stack Overflow
Where ChildComponent mounts, it fetches some data and then re-renders itself with the hydrated data. My unit test looks like: jest.mock('../../.
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
The problem is when the knowledge has been propogated differently;
https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#using-the-wrong-query https://testing-library.com/docs/guide-which-query/#priority
There’s absolutely nothing in the docs about the downsides of
ByRole
, and previously I’ve been happy to accept the small performance hit for the better testing methodology. But this issue is straight out problematic now.I would love to get an action on how best proceed with this, do we:
waitFor
intelligentlyByRole
to be asynchronousetc.
I just ran into this and found that switching from
findByRole
withname:
tofindByText
saved me over a second per test. I understand that*byRole
is more specific, and thus more reliable for getting the correct element. However, as far as “confidence” goes, I’m going to advise my team to avoidfindByRole
. The reason I found this performance issue is because I’m working on fixing a flaky test suite. The delay introduced byfindByRole
is causing our test suite to time out intermittently and therefore reduces confidence in our tests.I would highly recommend improving the performance of *byRole if possible.