Testing Custom Hooks
Learn how to test custom hooks in isolation using renderHook without needing a full component to host them.
Custom hooks can't be called outside of a React component's render, which means testing one directly by simply invoking it as a plain function fails — it needs to run inside React's rendering machinery to correctly manage its internal hook state. Testing Library provides renderHook (from @testing-library/react) specifically to solve this: it mounts a minimal, invisible test component that calls your hook and exposes its return value for assertions.
renderHook is like a minimal test rig that plugs your engine (the custom hook) into just enough of a car chassis (a bare test component) to actually start it up and observe its readings, without needing to build and drive a fully finished vehicle (a real, complete UI component) just to verify the engine runs correctly.