React Curriculum

React Testing & Debugging Interview Questions

React testing & debugging interview questions reward engineers who write resilient, maintainable test suites that test user behavior rather than implementation details. High-bar interview panels evaluate your approach to testing asynchronous interactions and custom hooks.

Master React Testing Library query hierarchies prioritizing accessible roles, eliminate act() warnings when testing async fetch requests with Mock Service Worker, test custom hooks using renderHook, and identify detached DOM nodes causing memory leaks.

The lessons follow React Testing Library's rule of testing what the user sees: querying by role and label instead of class names, testing async components against a mocked network with Mock Service Worker, and testing custom hooks with renderHook and act(). The last lesson moves from tests to debugging: finding memory leaks caused by effects that never clean up and closures that keep old state alive.

Interviewers use these questions to tell apart candidates whose tests break on every refactor from those whose tests fail only when the behaviour does.

React Testing & Debugging Interview Questions: 4 lessons

Frequently asked questions

What is the difference between getBy, queryBy and findBy in React Testing Library?

getBy throws if the element is missing, so use it when the element must be there. queryBy returns null instead, so use it to assert that something is absent. findBy returns a promise and retries until the element appears, so use it for content that renders after an async update.

Why use Mock Service Worker instead of mocking fetch?

MSW intercepts requests at the network level, so the component's real fetch or axios code runs unchanged, and the same mocks work in tests, in the browser and in Storybook. Mocking fetch directly ties a test to how a request is made rather than to what it returns.

What causes memory leaks in React?

Effects that subscribe to something, such as an event listener, an interval, a WebSocket or a store, without returning a cleanup function, and the closures held by those subscriptions, which keep old props, state and DOM nodes reachable after the component unmounts.