Sub topic 12
Custom render helpers for providers
This chapter assumes you can write a basic React Testing Library test with render and screen. It also assumes you know React context and providers.
The problem: components that need a provider
Many components read from context, such as a theme, a router, or a store. Rendering one without its provider fails or gives the wrong output. Wrapping every test by hand works, but it repeats the same setup everywhere.
Here is a small theme context and a component that uses it.
// ThemeProvider.jsx
import { createContext, useContext } from 'react';
const ThemeContext = createContext('light');
export function ThemeProvider({ children, theme = 'light' }) {
return <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider>;
}
export function useTheme() {
return useContext(ThemeContext);
}
// ThemeLabel.jsx
import { useTheme } from './ThemeProvider';
export function ThemeLabel() {
const theme = useTheme();
return <p>Current theme: {theme}</p>;
}The wrapper option
render accepts a wrapper option. It is a component that receives children, and Testing Library renders your UI inside it. A helper that sets this option once looks like this:
// test-utils.jsx
import { render } from '@testing-library/react';
import { ThemeProvider } from './ThemeProvider';
export function renderWithTheme(ui, options) {
return render(ui, { wrapper: ThemeProvider, ...options });
}Spreading options last lets a test override the wrapper when it needs to. Your test then calls the helper instead of render:
// ThemeLabel.test.jsx
import { screen } from '@testing-library/react';
import { renderWithTheme } from './test-utils';
import { ThemeLabel } from './ThemeLabel';
test('shows the default theme', () => {
renderWithTheme(<ThemeLabel />);
expect(screen.getByText('Current theme: light')).toBeInTheDocument();
});Passing per-test settings
A fixed wrapper cannot vary between tests. To choose the theme per test, build the wrapper inside the helper from a custom option. Pull your own option out of options so that it never reaches render.
// test-utils.jsx
import { render } from '@testing-library/react';
import { ThemeProvider } from './ThemeProvider';
export function renderWithTheme(ui, { theme = 'light', ...options } = {}) {
function Wrapper({ children }) {
return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
}
return render(ui, { wrapper: Wrapper, ...options });
}test('uses the dark theme when asked', () => {
renderWithTheme(<ThemeLabel />, { theme: 'dark' });
expect(screen.getByText('Current theme: dark')).toBeInTheDocument();
});Combining several providers
Real apps usually need more than one provider. Nest them in a single wrapper component. Create fresh state, such as a new store or query client, inside the helper so no test leaks state into the next one.
Re-exporting from one module
Some teams re-export everything from Testing Library and replace render. Tests then import from one place, and the custom render is used by default.
// test-utils.jsx (addition)
export * from '@testing-library/react';
export { renderWithTheme as render };A local named export wins over a name that comes from export *, so tests that import render from this file get the wrapped version. The trade-off is that a plain render call no longer shows which providers are in use. Some readers find an explicit name like renderWithTheme clearer.
Common mistakes
- Calling
renderinside the wrapper. The helper should only pass options torender. - Sharing one store or client object across tests. State from one test then changes the results of another.
- Forgetting
childrenin the wrapper component, so nothing renders. - Wrapping providers that a test does not need. This hides missing-provider bugs that you would want to catch.
Exercise
Write a helper signature for renderWithTheme so that a test can call renderWithTheme(<ThemeLabel />, { theme: 'dark' }). Why must theme be removed from the options before they are passed to render?
Show answer
Use function renderWithTheme(ui, { theme = 'light', ...options } = {}). theme is your own option and not a render option. If it were passed through, render would ignore it and the provider would never get the value. Pulling it out lets you pass it to ThemeProvider inside a wrapper component.
Exercise
Why should a helper that wraps a Redux-style store create the store inside the helper function and not at the top of the module?
Show answer
A store created at module level is shared by every test in the file, so state from one test leaks into the next. Creating it inside the helper gives each test a fresh store.