arrow_backBACK TO DRILLS
Blue BeltuseeffectPREMIUM
waitFor and act Exist Because React State Updates Are Asynchronous
A test renders a component, fires a click that calls setState inside the handler, then immediately asserts on the resulting text.
REACT_MODULE // 0x42
test('updates count on click', async () => {
render(<Counter />);
const button = screen.getByRole('button');
await fireEvent.click(button);
expect(screen.getByText('1')).toBeInTheDocument();
});Even though the click is awaited, why might this assertion fail intermittently unless wrapped in waitFor()?