React-hooks-testing-library: TypeError: Cannot read property 'current' of undefined

Created on 22 Aug 2019  路  3Comments  路  Source: testing-library/react-hooks-testing-library

  • @testing-library/react-hooks version: 2.0.1
  • react-test-renderer version: 16.8.6
  • react version: 16.8.6
  • node version: v12.9.0
  • npm version: 6.10.2

Relevant code or config:

import { renderHook } from '@testing-library/react-hooks';
import { useMyHook } from '../useMyHook';

test('useMyHook initializes correctly', () => {
  const { result } = renderHook(() => useMyHook());
});

What you did:

simply run the tests react-scripts test --env=jsdom --watchAll

What happened:

It throw an error saying it can not read property 'current'. I searched most places and I didn't see anyone else getting the same error.

TypeError: Cannot read property 'current' of undefined

      12 | test('useMyHook initializes correctly', () => {
    > 13 |   const { result } = renderHook(() => useMyHook());
                                ^

Reproduction:

https://codesandbox.io/s/kind-agnesi-v9ren

Problem description:

Because it doesn't seem to be possible to test hooks with this library.
Is there something missing in the docs that needs to be done as an extra step?

bug

Most helpful comment

The sandbox you linked has misatching versions for react, react-dom and react-test-renderer.

Updating them all to 16.9.0 makes the test pass. https://codesandbox.io/s/lively-moon-05d78

All 3 comments

The sandbox you linked has misatching versions for react, react-dom and react-test-renderer.

Updating them all to 16.9.0 makes the test pass. https://codesandbox.io/s/lively-moon-05d78

thanks @mpeyper. can confirm

was seeing error with:

    "react": "^16.6.1",
    "react-test-renderer": "^16.8.6",
    "@testing-library/react": "^9.1.3",
    "react-dom": "^16.6.1",

no longer with:

    "react": "^16.9.0",
    "react-test-renderer": "^16.9.0",
    "@testing-library/react": "^9.1.3",
    "react-dom": "^16.9.0",

make sure to install the same verions of react and react-test-renderer (e.g. 16.8.3).

Was this page helpful?
0 / 5 - 0 ratings