- React
- Unit Testing, Async Mocking, and Debugging Workflows
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
React Testing Library: Philosophy & Queries
React Testing Library tests components from the user perspective rather than implementation details. It avoids testing internal state or component instances, enforcing an accessible query hierarchy (getByRole > getByLabelText > getByText > getByTestId) and using userEvent over fireEvent.
Testing Async Components, Fetch Mocking & MSW
Testing async components requires intercepting network requests at the network layer using Mock Service Worker (MSW) rather than mocking fetch or axios globals. Use findBy queries to await rendered asynchronous data and assert loading, success, and error boundary states.
Testing Custom Hooks with renderHook & act
Custom hooks must be tested within a React component execution context. React Testing Library provides renderHook to mount a test wrapper, expose result.current, and rerun hooks on rerender. Any hook functions that trigger state updates must be wrapped in act() to ensure React flushes all scheduled work before assertions.
Debugging React Memory Leaks & Retained Closures
Debugging React Memory Leaks & Retained Closures: memory leaks in React occur when unmounted components leave active references that prevent garbage collection. Common causes include uncleared setInterval/setTimeout, forgotten window event listeners in useEffect, uncleared WebSocket/subscription listeners, and closures retaining large data objects.
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.
