React: React-Test-Renderer@16 breaks trying to use internal property

Created on 11 Oct 2017  Ā·  18Comments  Ā·  Source: facebook/react

Using version 16.0.0, I get a TypeError: Cannot read property 'ReactCurrentOwner' of undefined

Sample test case https://github.com/eddiemonge/snapshots-broken but really its just:

```package.json
{
"scripts": {
"test": "jest"
},
"devDependencies": {
"jest": "^21.2.1",
"react": "^16.0.0",
"react-test-renderer": "^16.0.0"
}
}

```test.spec.js
var ReactTestRenderer = require('react-test-renderer');

Error thrown is:

 FAIL  ./test.spec.js
  ā— Test suite failed to run

    TypeError: Cannot read property 'ReactCurrentOwner' of undefined

      at node_modules/react-test-renderer/cjs/react-test-renderer.development.js:622:36
      at Object.<anonymous> (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:8255:3)
      at Object.<anonymous> (node_modules/react-test-renderer/index.js:6:20)
      at Object.<anonymous> (test.spec.js:1:114)
          at Generator.next (<anonymous>)
          at Promise (<anonymous>)
          at Generator.next (<anonymous>)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

Offending code is

var ReactInstanceMap_1 = ReactInstanceMap;

var ReactInternals = react.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;

var ReactGlobalSharedState = {
  ReactCurrentOwner: ReactInternals.ReactCurrentOwner
};

Seems like maybe from this change? https://github.com/facebook/react/pull/9366

Most helpful comment

Make sure react-test-renderer version is the same with react, react-dom version

All 18 comments

This code alone doesn’t reproduce. Jest says:

 FAIL  ./test.spec.js
  ā— Test suite failed to run

    Your test suite must contain at least one test.

šŸ˜‰

I modified the code to:

var React = require('react');
var ReactTestRenderer = require('react-test-renderer');

it('does things', () => {
  var inst = ReactTestRenderer.create(React.createElement('h1', null, 'qwe'));
  expect(inst.toJSON()).toMatchSnapshot();
})

It ran fine for me:

screen shot 2017-10-12 at 12 00 02 pm

My guess is something is wrong with your environment, and you don’t actually have react@16 there. Maybe you have a bad package-lock.json or something like this.

Make sure you’re using react@16 and it should work.

Happy to reopen if you can provide a reproducing example that does reproduce the bug.

I did provide a reproducing example in the repo. I did a fresh clone of it, changed the test file to the code you had, ran npm install, and then npm test and still got the same thing.

here's a log of it https://gist.github.com/eddiemonge/879043c56c3f1d5a26f34ba94ef9d616

I don't generate package.lock files by default so its not that. You can see the versions that got installed.

@gaearon please reopen

@eddiemonge what verisons of node and npm are you using? I also tried to repro with both yarn and npm and was unable to trigger that error, but only see the output @gaearon noted.

Hmm ok then I'll troubleshoot more on my side

Im getting the same error -' TypeError: Cannot read property 'ReactCurrentOwner' of undefined' if I just add 'import renderer from 'react-test-renderer' on a test file my dependencies are as follows:

{
  "name": "testapp",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^15.6.2",
    "react-dom": "^15.5.4",
    "react-tap-event-plugin": "^2.0.1",
    "react-test-renderer": "^16.0.0"
  },
  "devDependencies": {
    "react-scripts": "0.9.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "flow": "flow"
  }
{
  "name": "testapp",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^15.6.2",
    "react-dom": "^15.5.4",
    "react-tap-event-plugin": "^2.0.1",
    "react-test-renderer": "^16.0.0"
  },
  "devDependencies": {
    "react-scripts": "0.9.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "flow": "flow"
  }

please find a screen cast description here ..
https://www.dropbox.com/s/eu365xtwd3jl458/renderer%20reactcurrentOwner.wmv?dl=0

@eddiemonge
Thank you for any help
Naveen

Hey @eddiemonge I changed the version of react and react-test-renderer to 15.4.0 and did npm install...it did work!

I had an older version of React/test installed globally somehow. removing those, forcing a cache clean, and then reinstalling locally fixed the problem. so i think something was trying to use the global version

@eddiemonge Thank you for discovering this. I had a similar issue and removing the global react package allowed my tests to run. For reference, here's the issue I saw:

image

@NaveenDK I ran into the same issue as you:

react-test-renderer's latest dist-tag is version 16.0.0 but requires a peer-dependency of react@^16.0.0-beta.5.

screen shot 2017-10-18 at 8 47 54 am

I was on react@^15.6.2 and kept running into the ReactCurrentOwner issue, but it looks like they just bumped react@latest to 16.0.0 which cleared everything up.

If you can't upgrade yet and need an older version of react-test-render you can find an older, compatible version on the registry at https://registry.npmjs.org/react-test-renderer/

The peer dependency was wrong—please disregard it and install 16.0.0.

Make sure react-test-renderer version is the same with react, react-dom version

I ran into a similar issue with a dependency conflict. As a part of the Jest install, I installed version 16.0.0 of react-test-render while I had react at 15.6.2. I was able to resolve this by bumping down the version of react-test-render.

Make sure react-test-renderer version is the same with react version

Why isn't this documented anywhere? Shouldn't it be documented on the react-test-renderer npm page?

It is documented in a form of peer dependency. If you install react-test-renderer with a wrong version of react, your package manager will warn you that it's not supported.

As of React 16, they don't have to be exact. [email protected] should work with any version of react that also has 16 as a major.

You're welcome to submit a PR to the documentation. It's here: http://github.com/reactjs/reactjs.org.

Got the same error with react, react-dom and react-test-renderer of the same version (16.3.2) :(

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

Please file a new issue with reproducing example.

I’m locking this one because ā€œI have this tooā€ replies don’t help anyone fix the problem. If there’s a bug we can’t fix it without a reproducing example.

Update: here’s one more possible case how you could get this https://github.com/airbnb/enzyme/issues/1623

Thanks @1999 for the tip!

Was this page helpful?
0 / 5 - 0 ratings