Explore Library
Code Quiz

Testing a click with user-event

Spot the async mistake when simulating a click with React Testing Library and user-event v14.

Codejavascript
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import Counter from './Counter';

test('increments count on click', async () => {
  render(<Counter />);

  const button = screen.getByRole('button', { name: /increment/i });
  userEvent.click(button);

  expect(screen.getByText('Count: 1')).toBeInTheDocument();
});

This test intermittently fails to see the updated count. What is the bug?