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

Created on 12 Aug 2019  路  5Comments  路  Source: testing-library/react-testing-library

When i write even the simplest of tests i get an undefined error.

Stacktrace:

TypeError: Cannot read property 'current' of undefined

    at act (C:\Repositories\secata\npm\react-test\node_modules\react-dom\cjs\react-dom-test-utils.development.js:983:55)
    at act (C:\Repositories\secata\npm\react-test\node_modules\react-dom\cjs\react-dom-test-utils.development.js:1418:12)
    at Object.render (C:\Repositories\secata\npm\react-test\node_modules\@testing-library\react\dist\pure.js:82:26)
    at Object.<anonymous> (C:\Repositories\secata\npm\react-test\src\test\Test.test.tsx:10:23)
    at step (C:\Repositories\secata\npm\react-test\src\test\Test.test.tsx:32:23)
    at Object.next (C:\Repositories\secata\npm\react-test\src\test\Test.test.tsx:13:53)
    at C:\Repositories\secata\npm\react-test\src\test\Test.test.tsx:7:71
    at new Promise (<anonymous>)
    at Object.<anonymous>.__awaiter (C:\Repositories\secata\npm\react-test\src\test\Test.test.tsx:3:12)
    at Object.<anonymous> (C:\Repositories\secata\npm\react-test\src\test\Test.test.tsx:8:20)
    at Object.asyncJestTest (C:\Repositories\secata\npm\react-test\node_modules\jest-jasmine2\build\jasmineAsyncInstall.js:102:37)
    at resolve (C:\Repositories\secata\npm\react-test\node_modules\jest-jasmine2\build\queueRunner.js:43:12)
    at new Promise (<anonymous>)
    at mapper (C:\Repositories\secata\npm\react-test\node_modules\jest-jasmine2\build\queueRunner.js:26:19)
    at promise.then (C:\Repositories\secata\npm\react-test\node_modules\jest-jasmine2\build\queueRunner.js:73:41)
    at process._tickCallback (internal/process/next_tick.js:68:7)

Dependencies:

  "dependencies": {
    "react": "^16.6.3",
    "react-dom": "^16.9.0"
  },
  "devDependencies": {
    "@testing-library/dom": "^6.0.0",
    "@testing-library/jest-dom": "^4.0.0",
    "@testing-library/react": "^9.1.0",
    "@types/jest": "^24.0.17",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.8.5",
    "jest": "^24.8.0",
    "ts-jest": "^24.0.2",
    "tslint": "^5.18.0",
    "typescript": "^3.5.3"
  },

Test:

import {waitForElement} from "@testing-library/dom";
import "@testing-library/jest-dom/extend-expect";
import {render} from "@testing-library/react";
import "@testing-library/react/cleanup-after-each";
import * as React from "react";

it("Hello World!", async () => {
  // Render new instance in every test to prevent leaking state
  const {getByText} = render(<div>Hello World!</div>);

  await waitForElement(() => getByText(/Hello World!/i));
});

Most helpful comment

Oops.

Updated react to 16.9.0 and it started working again.

All 5 comments

Oops.

Updated react to 16.9.0 and it started working again.

Same here using react-native. Changed from 16.8.3 to 16.9 and now works.

Also note that everything must have the same version:

"react": "16.9",
"react-dom": "16.9",
"react-test-renderer": "^16.9",
"@types/react": "^16.9",

TY @libasoles this saved me today 馃檹 upgraded from 16.8.6 to 16.9 for both react and react-dom

@libasoles I have same problem but i can't upgrade my react from 16.8.6 to 16.9. any other solutions ?

@PIMBA This is late, but your react and react-dom should be the same version

Was this page helpful?
0 / 5 - 0 ratings