Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Testing a custom hook fails after upgrading to React 18

ErrorrenderHook from @testing-library/react-hooks fails with React 18

Short answer

The package @testing-library/react-hooks is deprecated and aimed at older React. Since React Testing Library 13.1, renderHook ships inside @testing-library/react. Change the import, remove the old package and update to a current version of @testing-library/react.

  • Applies to @testing-library/react 16, React 19, Jest 29
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which testing packages and which React version you have.

    npm ls react @testing-library/react @testing-library/react-hooks
  2. 2Match what you see, then jump to its fix

    1. 1Imports mention @testing-library/react-hooksThe test imports renderHook from the old packageFix
    2. 2renderHook is not exported even though you import it from @testing-library/react@testing-library/react is older than 13.1Fix
    3. 3result.current is stale or a wrapper receives no propsReading the result or props the old wayFix

What does the error look like?

  • renderHook from @testing-library/react-hooks fails with React 18
  • renderHook import still comes from @testing-library/react-hooks

Cause 1 of 3

The test imports renderHook from the old package

1Check

Search the tests for the old import.

grep -rn "react-hooks" src/ package.json

2Fix

Import from @testing-library/react and uninstall the old package.

npm uninstall @testing-library/react-hooks
// in the test file
import { renderHook } from '@testing-library/react';

3Verify

Run the hook tests.

npx jest

Confirmed by github.com

Did this fix it?

Cause 2 of 3

@testing-library/react is older than 13.1

1Check

Look at the installed version.

npm ls @testing-library/react

2Fix

Upgrade to 13.1 or newer.

npm install --save-dev @testing-library/react@latest

3Verify

The import resolves.

npx jest

Confirmed by github.com

Did this fix it?

Cause 3 of 3

Reading the result or props the old way

1Check

renderHook returns result as a ref, so read result.current for the latest value. initialProps go to the callback, not to the wrapper.

grep -rn "initialProps" src/

2Fix

Use rerender with new props and read result.current afterward. Build the wrapper with a factory if it needs props.

const { result, rerender } = renderHook(({ id }) => useUser(id), { initialProps: { id: 1 } });
rerender({ id: 2 });
expect(result.current.id).toBe(2);

3Verify

The assertion passes.

npx jest

Confirmed by testing-library.com

Did this fix it?

How do I stop it coming back?

  • Keep @testing-library/react current and drop packages marked deprecated.
  • Test the hook through renderHook and a rerender call, not a throwaway component, unless it needs a provider.

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 built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.