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

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:

{
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "jest": "^21.2.1",
    "react": "^16.0.0",
    "react-test-renderer": "^16.0.0"
  }
}
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

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (1 by maintainers)

Most upvoted comments

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

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.

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. react-test-renderer@16.x 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.

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