Name a common way to test React components.
Options
- Jest or Vitest with React Testing Library (Correct option)
- Only manual testing carried out in the browser
- Maven Surefire, run from the backend project
- Selenium only, used for every kind of test
Answer
Jest (or Vitest) with React Testing Library, which tests behavior the way a user sees it
Why
Jest or Vitest runs tests and provides assertions and mocks. React Testing Library renders components and lets you find elements like a user would (getByRole, getByText) and trigger events (userEvent.click).
Principle: test what the user sees and does, not internal state or implementation details.
End-to-end tests in a real browser use Selenium, Playwright or Cypress.
Remember
Jest/Vitest + React Testing Library for components; Playwright/Selenium for E2E.