Asserting that a React component throws in Jest and Testing Library
Fix it fast
Most common cause, 1 of 3render is called before expect can catch the error
Wrap the call in an arrow function.
test('throws without a user', () => {
expect(() => render(<Profile user={null} />)).toThrow('Profile needs a user');
});
test('useCart outside CartProvider', () => {
expect(() => renderHook(() => useCart())).toThrow(/CartProvider/);
});Short answer
To check that a React component throws, pass a function to expect: expect(() => render(<Profile />)).toThrow('message'). Testing Library runs render inside act, and React 19 rethrows uncaught render errors from act in development. Received function did not throw means an error boundary caught it, or the throw happens outside render. Applies to React 19, RTL 16, Jest 30.
Which cause do you have?
Run only the failing test and read how it fails. If the component's own error message ends the test at the render line, see cause 1. If Jest prints Received function did not throw, add screen.debug() after render: fallback markup in the output means cause 2, normal markup means cause 3.
npx jest src/Profile.test.jsx -t "throws"Then find the line you see in this list and open its fix:
- 1render is called before expect can catch the error
The test fails with the component's message, for example Profile needs a user, pointing at the line that calls render, with no matcher diff - 2An error boundary in the rendered tree catches the error
Received function did not throw, screen.debug() shows fallback markup, and the console shows The above error occurred in the <Profile> component - 3The throw happens in an event handler or async code
Received function did not throw, screen.debug() shows normal markup, and the error only appears after a click, a timer or an awaited request
What does the error look like?
Received function did not throwexpect(received).toThrow(expected)React will try to recreate this component tree from scratch using the error boundary you provided
Cause 1 of 3
render is called before expect can catch the error
You seeThe test fails with the component's message, for example Profile needs a user, pointing at the line that calls render, with no matcher diff
Check
A thrown render error that fails the test outright means render ran as an argument to expect, not inside a callback. Jest's toThrow docs say the code must be wrapped in a function, or the error escapes before the matcher sees it. In development builds, React 19 hands uncaught errors raised inside act back to the caller, and Testing Library's render wraps root.render in act, so the error leaves render directly.
grep -rn "expect(render(\|expect(renderHook(" src/Fix
Wrap the call in an arrow function. The same applies to a hook that throws when it is used outside its provider.
test('throws without a user', () => {
expect(() => render(<Profile user={null} />)).toThrow('Profile needs a user');
});
test('useCart outside CartProvider', () => {
expect(() => renderHook(() => useCart())).toThrow(/CartProvider/);
});Verify
Both tests pass. Change the expected text on purpose once: Jest should now fail with a message diff, which proves the matcher really compares the error.
npx jest src/Profile.test.jsxConfirmed by jestjs.io
Cause 2 of 3
An error boundary in the rendered tree catches the error
You seeReceived function did not throw, screen.debug() shows fallback markup, and the console shows The above error occurred in the <Profile> component
Check
When a component throws during render but render itself returns normally, a boundary above it took the error and drew its fallback. React 19 sends errors that a boundary catches to console.error instead of rethrowing them. A shared test helper that passes a wrapper with providers and an ErrorBoundary is the usual hiding place.
grep -rn --include="*.test.jsx" --include="test-utils.jsx" "ErrorBoundary\|wrapper:" src/Fix
Assert what the user sees, the fallback, instead of expecting render to throw. Silence the expected console.error with a spy and restore it afterwards.
import { ErrorBoundary } from 'react-error-boundary';
test('shows the fallback when Profile throws', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
render(
<ErrorBoundary fallback={<p>Could not load profile</p>}>
<Profile user={null} />
</ErrorBoundary>,
);
expect(screen.getByText('Could not load profile')).toBeTruthy();
spy.mockRestore();
});Verify
The test passes, and the run prints no React error output for this case.
npx jest src/Profile.test.jsx -t "fallback"Confirmed by react.dev
Cause 3 of 3
The throw happens in an event handler or async code
You seeReceived function did not throw, screen.debug() shows normal markup, and the error only appears after a click, a timer or an awaited request
Check
React's error handling covers rendering, so a throw inside onClick, setTimeout or the callback of a rejected fetch never passes through render. The React docs list event handlers and asynchronous code among the things error boundaries do not catch, and render has already returned by the time those callbacks run.
grep -rn "throw " src/components/Profile.jsxFix
Route the error into the tree with showBoundary from react-error-boundary, or with a React 19 startTransition action, then wait for the fallback with a findBy query.
// Profile.jsx
import { useErrorBoundary } from 'react-error-boundary';
function Profile({ loadUser }) {
const { showBoundary } = useErrorBoundary();
useEffect(() => {
loadUser().catch(showBoundary);
}, [loadUser, showBoundary]);
return <p>Loading...</p>;
}
// Profile.test.jsx
test('a failed load shows the fallback', async () => {
const loadUser = jest.fn().mockRejectedValue(new Error('500'));
render(
<ErrorBoundary fallback={<p>Could not load profile</p>}>
<Profile loadUser={loadUser} />
</ErrorBoundary>,
);
expect(await screen.findByText('Could not load profile')).toBeTruthy();
});Verify
The async test passes, and it fails if loadUser is changed to resolve.
npx jest src/Profile.test.jsx -t "failed load"Confirmed by react.dev
How do I stop it coming back?
- Keep one helper for error-path tests that renders without the app-wide boundary, so a shared wrapper cannot swallow the error.
- Pair every toThrow on render with a second test that renders valid props and expects no throw, so a broken guard shows up.
- Use expect.assertions(1) in async error tests so a test that never reaches its assertion fails instead of passing.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, QistLock, Taskvare.