Description
React Testing Library
version: 10.0.4 (I'm using/react
not/dom
but I'm posting here asgetByRole
is implemented here)node
version: 🤷 (codesandbox)npm
(oryarn
) version: 🤷 (codesandbox)
I've noticed that some of my tests were pretty slow to run (more than 2 seconds). After investigating, I've noticed that the following pattern was to source of the slowness:
await waitForElementToBeRemoved(() => screen.getByRole('progressbar'))
If I add a data-testid
to my loader and switch to:
await waitForElementToBeRemoved(() => screen.getByTestId('loader'))
it's much faster.
Relevant code or config:
I'm sometimes displayed a CircularProgress
from material-ui
while fetching some data and I use this pattern to wait for it to be removed.
await waitForElementToBeRemoved(() => screen.getByRole('progressbar'))
example of implementation
if(isLoading) {
return <CircularProgress className={classes.loader} data-testid="loader" />
}
What happened:
Here are 2 tests outputs where I only change the pattern mentioned above.
Reproduction:
I've create the following Sandbox: https://codesandbox.io/s/musing-gauss-e1ynf?file=/src/Demo.js
The numbers are much smaller but when running the tests several times we can see that the getByRole
is slower when used on the CircularProgress
.
Problem description:
I think that
await waitForElementToBeRemoved(() => screen.getByRole('progressbar'))
is the best pattern to test that a loader is displayed on screen. However, the slowness prevents us from using in all our tests as it quickly gets slow.