We have noticed that our unit tests are performing very slowly and the culprit seems to be getByRole
for querying for button elements etc. Here is an example of a query that was giving us problems today:
userEvent.click(screen.getAllByRole('button', { name: 'Accept' })[0]);
Using console.time()
we measured this query as taking 12782ms to execute. After switching to getByText
the query takes 14ms, meaning that getByRole
is taking nearly 1000x longer (!!!).
I know that getByRole
is expected to run slower, but it seems extreme. I've found threads online detailing how some tests took 5x longer but nothing like this. Adding hidden: true
as an option speeds things up a little but not by a massive amount.
I'm unable to share the code that is being tested as it is private. I wouldn't describe the component as particularly complex, although it is comprised of a few lower-level components and uses mocked out network requests.
Are there any known reasons for slow running queries like this, or any ways we can speed up execution without sacrificing the confidence that getByRole
offers?
within
to only query within certain elements and avoid traversing the whole DOM? – Waller