CodePark/ questions / Async UI & act()
START PRACTICINGPRACTICE
Frontend question · Testing › Async UI & act()

What's wrong with this test?

await waitFor(async () => {
  await user.click(screen.getByRole("button", { name: /load more/i }));
  expect(screen.getAllByRole("listitem")).toHaveLength(20);
});
waitFor retries the callback until it passes, so the click can run many times; perform the click before waitFor

waitFor runs its callback again and again until it stops throwing or times out. So any side effect inside it, like the click, runs on every retry. You might load several pages. Do the interaction once, outside waitFor. Put only the assertion inside waitFor, or use findAllByRole. The option saying waitFor callbacks must not contain assertions is wrong. Assertions are exactly what belongs there.