⚛️ React.js Intermediate

What is React Testing Library?

Why Interviewers Ask This

Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.

Answer

React Testing Library (RTL) is the official recommended testing utility for React. Its philosophy: test components the way users interact with them — not implementation details. RTL renders components in a virtual DOM (via jsdom) and provides queries to find elements like a user would: by text, role, label, placeholder — not by class names or component internals. Key queries: getByRole("button", { name: "Submit" }), getByLabelText("Email"), getByText("Welcome"), findByText("Loaded!") (async). Key actions: userEvent.click(button), userEvent.type(input, "hello"). Assertions: with jest-dom: expect(element).toBeInTheDocument(), toHaveValue("hello"), toBeDisabled(). Philosophy benefits: tests do not break when you refactor internals (change class names, extract components) — they only break when behavior changes. This produces more durable, maintainable tests. Avoid: testing component state directly, implementation details, or internal methods. Instead, test what the user sees and can do.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.