Testing Context Providers and Custom Render Wrappers
Learn how to test components that depend on Context by rendering them within their required providers, using a reusable custom render function.
Components that consume Context (theme, auth, a query client, i18n) will throw an error or behave incorrectly if rendered directly in a test without their required Provider ancestor, since useContext would return the default value (or nothing meaningful) instead of the value the component actually depends on in the real app. Tests need to wrap the component under test in the same providers the real application tree supplies.
A custom render wrapper is like a stage crew that automatically sets up the same standard backdrop and lighting rig (providers) before every scene (test) is filmed, so each individual director (test file) doesn't have to redundantly instruct the crew to reassemble the same set from scratch every single time, while still being able to request a specific lighting change for one particular scene.