Cannot find module 'react/lib/ReactComponentTreeHook' from 'ReactDebugTool.js'

I'm trying to get Jest to run a snapshot test of my React app. The versions from my package.json:

  "react": "15.6.1",
  "react-dom": "15.6.1",
  "react-test-renderer": "15.6.1",

I can't get past this error:

● Test suite failed to run


Cannot find module 'react/lib/ReactComponentTreeHook' from 'ReactDebugTool.js'

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
  at Object.<anonymous> (node_modules/react-test-renderer/lib/ReactDebugTool.js:16:30)

I have tried removing and reinstalling my node_modules dir and I've verified that the path to my component is correct but still get this same error.

My test looks like this:

import React from 'react';
import renderer from 'react-test-renderer';
import { Section } from '../../app/views/containers/section';

it('renders correctly', () => {
  const section = renderer.create(
    <Section key="1" section="finance"/>
  ).toJSON();
  expect(section).toMatchSnapshot();
});

What am I doing wrong?


Solution 1:

Ran into the similar problem last week, we have a React-Native project that has recently upgraded to:

"react-native": "0.45.1"
"react": "16.0.0-alpha.12"
"jest": "20.0.4"
"react-test-renderer": "15.5.4"

and then we try to run our Jest tests and we saw the same issue as you mentioned above. Then we realized there is a cutting edge version of the react-test-renderer and we tried that one out:

"react-test-renderer": "^16.0.0-alpha.12",

And now the issue is no longer there.

Solution 2:

On 0.47.0

Still had errors with the accepted answer had to do the following:

"react-dom": "^16.0.0-beta.5", "react-test-renderer": "16.0.0-alpha.12",

enzyme will work with the above changes but any sort of simulation will not, disabled taps until they support.

Solution 3:

While upgrading to React 16.0.0, I did notice that you do need to upgrade react-dom to 16.0.0 and it works flawless!