question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

9reactions
davidnguyen11commented, Nov 29, 2017

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

2reactions
gaearoncommented, Oct 12, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test Renderer - React
This package provides a React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM...
Read more >
How to test a react component that is dependent on ...
There's a number of ways to make this work, but only approach I've found that works with 'react-test-renderer/shallow' is to inject a mock...
Read more >
react-test-renderer | Yarn - Package Manager
Design simple views for each state in your application, and React will efficiently update and render just the right components when your data...
Read more >
How to Test React Components: the Complete Guide
unit testing: testing an isolated part of your app, usually done in combination with shallow rendering. example: a component renders with the ...
Read more >
Testing React Components with react-test-renderer, and the ...
Snapshot testing is a feature built into the Jest test runner and since it's the default library for testing React we'll make use...
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