Antonis Christofides
Antonis Christofides

Reputation: 6949

TypeError: Cannot read property 'ReactCurrentOwner' of undefined

I'm running the unit tests in a react-native app I'm developing and three of them are failing with this message:

TypeError: Cannot read property 'ReactCurrentOwner' of undefined

The error does not seem to be caused by my code, but either by react-test-renderer (in two tests) and in enzyme-adapter-react-16 (in one test).

This was an expo app and the unit tests were running. I then ejected. I replaced

"jest": {
  "preset": "jest-expo"
},

with

"jest": {
  "preset": "react-native"
},

and all my other tests are running. The three failing tests are the only ones that use react-test-renderer and enzyme-adapter-react-16.

I've tried (many times) removing node_modules and reinstalling. Here are the versions I'm using:

"devDependencies": {
  "babel-preset-react-native-stage-0": "^1.0.1",
  "deep-freeze": "^0.0.1",
  "enzyme": "^3.1.1",
  "enzyme-adapter-react-16": "^1.0.4",
  "eslint": "^4.9.0",
  "jest": "^21.2.1",
  "react-dom": "^16.0.0",
  "react-test-renderer": "^16.0.0"
},
"scripts": {
  "start": "react-native start",
  "android": "react-native run-android",
  "ios": "react-native run-ios",
  "test": "jest"
},
"jest": {
  "preset": "react-native"
},
"dependencies": {
  "assert": "^1.4.1",
  "react": "16.0.0",
  "react-native": "^0.50.1",
  "react-redux": "^5.0.6",
  "redux": "^3.7.2",
  "yarn": "^1.3.2"
}*

Upvotes: 7

Views: 20597

Answers (4)

ItSaulGoodMan
ItSaulGoodMan

Reputation: 66

For those using Expo and trying to update their React Native packages to the latest version – don't do what I did and manually update the packages (by doing pnpm up --latest for example).

Expo relies on packages being a certain version. Therefore, to solve the issue in this case:

  1. Let expo find issues with dependencies

npx expo-doctor

  1. Let expo fix the dependency issues identified above

npx expo install --check

If this still doesn't solve the issue, I would suggest performing a clean install of packages rm -rf node_modules, clearing the cache (for eg pnpm store prune), and trying the above steps again.

For those using a development build of expo, I'd suggest doing a clean prebuild npx expo prebuild --clean before trying to start the dev server (for eg npx expo run:ios).

Upvotes: 2

Francis Batista
Francis Batista

Reputation: 1520

Seems like you need to update react, react-dom and react-test-renderer to 16.3.2.

It seems like one of them is 16 alpha which is not officially supported yet.

"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-test-renderer": "^16.3.2"

More details below:

https://github.com/facebook/create-react-app/issues/2526#issuecomment-308081573

Upvotes: 2

Oleh
Oleh

Reputation: 612

I also had this issue and fixed it by installing react, react-dom and react-test-renderer of the same version.

Some background:

  • I've got one project where mentioned packages are 16.2.0 (they were
    installed initially with that version) and everything works fine.
  • On another project I had react and react-dom 15.4.2 and updating them to 16.2.0 by removing node_modules changing version in
    package.json and installing all dependencies from scratch didn't
    help. I had to roll react-test-renderer back to 15.4.2 and only
    then I managed to get this issue resolved. I don't know whether
    this is project or environment related issue but I never installed react or react-dom globally.

So try making react-test-renderer the same version as your react and react-dom.

Upvotes: 7

Antonis Christofides
Antonis Christofides

Reputation: 6949

For the benefit of others who have the same problem: it was fixed when I cleaned up my global node_modules and left react, react-native, and some other stuff only in the project's node_modules.

Upvotes: 2

Related Questions