Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest fails Cannot find module 'react/lib/React' from 'ReactShallowRenderer.js' after React 16 upgrade

Just upgraded to react v16.0.0 from v15.6.2 (via a rebase)

Problem

Running my jests tests gives the following error

  ● Test suite failed to run

    Cannot find module 'react/lib/React' from 'ReactShallowRenderer.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:191:17)
      at Object.<anonymous> (node_modules/react-test-renderer/lib/shallow/ReactShallowRenderer.js:16:13)

How I upgraded

Step 1: yarn.lock

During the rebase, onto a branch with React v16 - I let yarn 1.1.0 take care of merge conflicts on my yarn.lock file

Step 2: Add adapter

I added the new adapter setupTestFramework.js

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });

Step 3: upgrade packages

And upgraded the following packages:

react "16.0.0",
enzyme "3.1.0",
jest "21.2.1",
babel-jest "21.2.0",

Can anyone see anything I missed?

like image 718
Ashley Coolman Avatar asked Oct 11 '17 15:10

Ashley Coolman


2 Answers

When used with React 16, Enzyme requires v16.x.x of react-test-renderer to be installed.

like image 145
benceg Avatar answered Nov 10 '22 18:11

benceg


Worked for me by following below comand

npm uninstall react-dom  

and then

  npm install react-dom --save-dev 
like image 1
Amit Kumar Trivedi Avatar answered Nov 10 '22 20:11

Amit Kumar Trivedi