Cannot read properties of undefined (reading 'current')

See original GitHub issue
  • @testing-library/react-hooks version:7.0.2
  • react version:18.0.0-alpha-5ca4b0433-20211020
  • react-dom version (if applicable):18.0.0-alpha-5ca4b0433-20211020
  • react-test-renderer version (if applicable):17.0.1
  • node version:16.13.0
  • npm (or yarn) version: yarn version => 1.22.15
  • typescript version: ~4.4

Relevant code or config:

import { renderHook } from "@testing-library/react-hooks"

function useTest() {
    return { res: true }
}

describe('useTest', () => {
    it('should render', () => {
        const { result } = renderHook(() => useTest());
    });
});

What you did:

This is part of a Blitz.js app. This is a very minimal example. I started testing a more complex custom hook and i got in first place TypeError: Cannot read properties of undefined (reading ‘current’) so i decided to tear down to a bare minimal example.

What happened:

yarn test --verbose -t "useTest"
yarn run v1.22.15
$ jest --verbose -t useTest
 FAIL   CLIENT  app/githubs/hooks/useQueryBuilder.hook.test.ts
  useTest
    ✕ should render (7 ms)

  ● useTest › should render

    TypeError: Cannot read properties of undefined (reading 'current')

       7 | describe('useTest', () => {
       8 |     it('should render', () => {
    >  9 |         const { result } = renderHook(() => useTest());
         |                            ^
      10 |     });
      11 | });
      12 |

      at act (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15259:59)
      at renderHook (node_modules/@testing-library/react-hooks/lib/pure.js:148:30)
      at Object.<anonymous> (app/githubs/hooks/useQueryBuilder.hook.test.ts:9:28)

Test Suites: 1 failed, 5 skipped, 1 of 6 total
Tests:       1 failed, 20 skipped, 21 total

Reproduction:

Problem description:

Blitz.js comes with @testing-library/react-hooks:4.0.1 so i upgraded to 7.0.2 but nothing change.

Suggested solution:

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
mpeypercommented, Dec 14, 2021
  1. We don’t officially support React 18, and we likely never will. You should instead try the new renderHook API from react-testing-library (see here for details)
  2. I suspect the issue here is that you have not updated react-test-renderer to react 18 as well (the stack trace points to that as the detected renderer, but you are posting react-dom versions). Please see our docs about targeting specific renderers if you intend to use react-dom going forward.
5reactions
min9nimcommented, May 24, 2022

I have a same problem.

(my react version is 18.1.0)

so I upgraded “react-test-renderer” 17.0.2 to 18.1.0 and it works!

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read properties of undefined (reading 'and')
It keeps failing with this error in Karma. TypeError: Cannot read properties of undefined (reading 'and') import { ComponentFixture, TestBed } ...
Read more >
I need help. TypeError: Cannot read ... - DEV Community ‍ ‍
I need help. TypeError: Cannot read properties of undefined (reading 'current'). This is code ⬇️⬇️. function QuizLogic(showScore, totalTime ...
Read more >
[SOLVED] Cannot Read Property of Undefined in JavaScript
The “cannot read property of undefined” error occurs when you attempt to access a property or method of a variable that is undefined...
Read more >
Cannot Read Property of Undefined in JavaScript - Rollbar
TypeError : Cannot read property of undefined occurs when a property is read or a function is called on an undefined variable.
Read more >
Uncaught TypeError: Cannot read property of undefined In
This error occurs in Chrome Browser when you read a property or call a method on an undefined object . There are a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found