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.

waitFor + getByRole causing severe delays

See original GitHub issue
  • @testing-library/dom version: 7.26.6
  • node 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.

image

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:

image

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:open
  • Created 3 years ago
  • Reactions:25
  • Comments:58 (30 by maintainers)

github_iconTop GitHub Comments

30reactions
herecydevcommented, Nov 13, 2020

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:

  1. auto-increment waitFor intelligently
  2. Change inner workings of ByRole to be asynchronous
  3. Provide documentation around this known issue with suitable guidance

etc.

26reactions
radfahrercommented, Mar 22, 2021

I just ran into this and found that switching from findByRole with name: to findByText 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 avoid findByRole. The reason I found this performance issue is because I’m working on fixing a flaky test suite. The delay introduced by findByRole 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.

Read more comments on GitHub >

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

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