How should I mock in React tests?
Mock at the boundary, not internal functions. Mock API calls, not the helper functions that call them. This keeps tests focused on behavior and lets internal refactors happen without breaking the mocks.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Unit Testing React Components: Best Practices That Survive Refactors
Test behavior, not implementation. Use role and text queries, avoid asserting on internal state or instance methods, and test the user flows. If a test breaks when you refactor without changing behavior, it is too coupled to implementation.
Because they query what the user experiences and double as accessibility checks. Test ids and class names are implementation details that can change during refactors, but roles and visible text are part of the user-facing behavior.
Because userEvent simulates real user interactions more accurately. It handles the full sequence of events a real user triggers, like focus and blur on a click, giving more realistic and reliable tests than fireEvent.
Still have questions?
Browse all our FAQs or reach out to our support team
