React Testing Library encourages tests that resemble how users interact with your UI. Interviewers ask about query priority and avoiding implementation details.
Common Interview Questions
Q: Query priority order?
Answer: Prefer getByRole, getByLabelText, getByPlaceholderText, then getByText. Use getByTestId last. Accessibility-first queries make better tests.
Q: getBy vs findBy vs queryBy?
Answer: getBy throws if missing (sync). queryBy returns null. findBy returns Promise for async appearance — use with await and waitFor.
Q: User event example
Answer: Simulate real interactions. Prefer @testing-library/user-event over fireEvent for realistic input.
import userEvent from "@testing-library/user-event";
await userEvent.type(screen.getByLabelText(/email/i), "a@b.com");
await userEvent.click(screen.getByRole("button", { name: /submit/i }));
expect(await screen.findByText(/success/i)).toBeInTheDocument();
Q: What not to test?
Answer: Internal state, private methods, or implementation details like shallow rendering child components. Test visible outcomes and behavior.
Key Points for Interviews
-
Test like a user, not like a developer
-
Mock network at MSW/handler level
-
wrap renders with providers (router, theme)
-
avoid snapshot-only tests for logic
-
async tests need findBy/waitFor
