Question presented to candidate: "How do you decide what to test in a React component, and what do you avoid testing?"
What a strong answer should cover:
- Test behaviour, not implementation. Render the component, interact with it the way a user would, and assert on what is visible — not on state, not on which hooks ran.
- The concrete test of whether you got that right: a pure refactor must not break the test. If restyling the markup breaks it, the test was coupled to implementation.
- Query priority matters: prefer accessible queries — by role with an accessible name, by label, by text — over test ids, and never CSS classes. Role queries double as an accessibility check.
getBythrows if not found,queryByreturns null (use it to assert absence),findByis async (use it for anything that appears later).- Use
userEventover rawfireEventwhere available — it simulates the full interaction sequence rather than dispatching one synthetic event. - The pyramid in practice: many small unit/component tests, some integration tests across a few components, few end-to-end tests for critical journeys.
- Do not test the library — that a
useStateupdates is React's problem. Test the behaviour it produces. - Mock at the network boundary rather than mocking your own modules, so the test exercises real component wiring.
Clarifying questions expected:
- "Is this a shared component or a page-level flow?" — that changes the level to test at.
- "Do we have accessible names on these controls?" — if not, the test problem is really an accessibility problem.
Code / implementation expected: Optional. One good test is more convincing than a description.