Enzyme: can not find module 'react-addon-test-utils' from 'node_modules/enzyme/build'

Created on 12 May 2017  路  12Comments  路  Source: enzymejs/enzyme

Our project is using :
React: ^15.5.4
react-dom: ^15.5.4
react-test-renderer: ^15.5.4

according to airbnb/enzyme website, If you are using React >=15.5, in addition to enzyme, you will have to ensure that you also have the react-dom and react-test-renderer npm modules installed, we already installed these npm modules, only that in React 0.14 or React <15.5 need to install the react-addons-test-utils.
when we use enzyme to test our react component, there is a problem:
can not find module 'react-addon-test-utils' from 'node_modules/enzyme/build'.
If we install the module 'react-addon-test-utils', then the test is OK, but our React >=15.5, Why we need to install the react-addon-test-util?

Anybody has solution to this problem? Thanks~

Most helpful comment

@ljharb same situation here,

react: ^15.5.4,
react-dom: ^15.5.4,
react-test-renderer: ^15.5.4,
enzyme: ^2.8.2

and getting:

ERROR in ./~/enzyme/build/react-compat.js
Module not found: Error: Can't resolve 'react-addons-test-utils' in '/Users/.../node_modules/enzyme/build'
 @ ./~/enzyme/build/react-compat.js 143:20-54

I'm also able to directly require react-dom/test-utils and react-test-rendered/shallow.

In my case, I'm using karma with karma-webpack.

Adding this to my webpack config removed the warnings:

externals: {
  cheerio: 'window',
  'react/addons': 'react',
  'react/lib/ExecutionEnvironment': 'react',
  'react/lib/ReactContext': 'react',
  'react-addons-test-utils': 'react-dom', // <- added this
},

All 12 comments

What version of enzyme are you using?

I have the same set up, and have tried enzyme 2.8.0, 2.8.1, 2.8.2 to no avail :(

react-addons-test-utils is only attempted to be required when either of these two requires fail.

With v2.8.2 installed, if you try to require those things in the node repl, what happens?

Still fails with:
react: ^15.5.4,
react-dom: ^15.5.4,
react-test-renderer: ^15.5.4,
enzyme: ^2.8.2

on my Mac and on Linux:

WARNING in ./~/encoding/lib/iconv-loader.js
Critical dependencies:
9:12-34 the request of a dependency is an expression
 @ ./~/encoding/lib/iconv-loader.js 9:12-34

ERROR in ./~/enzyme/build/react-compat.js
Module not found: Error: Cannot resolve module 'react-addons-test-utils' in /Users/../.node_modules/enzyme/build
 @ ./~/enzyme/build/react-compat.js 143:20-54

And in the node repl, if you type require('react-dom/test-utils') and require('react-test-renderer/shallow'), what happens?

(also, just to confirm, npm ls is existing zero?)

npm ls returns no errors. I have also installed [email protected] and still see the same error.

And what about those repl requires?

@ljharb same situation here,

react: ^15.5.4,
react-dom: ^15.5.4,
react-test-renderer: ^15.5.4,
enzyme: ^2.8.2

and getting:

ERROR in ./~/enzyme/build/react-compat.js
Module not found: Error: Can't resolve 'react-addons-test-utils' in '/Users/.../node_modules/enzyme/build'
 @ ./~/enzyme/build/react-compat.js 143:20-54

I'm also able to directly require react-dom/test-utils and react-test-rendered/shallow.

In my case, I'm using karma with karma-webpack.

Adding this to my webpack config removed the warnings:

externals: {
  cheerio: 'window',
  'react/addons': 'react',
  'react/lib/ExecutionEnvironment': 'react',
  'react/lib/ReactContext': 'react',
  'react-addons-test-utils': 'react-dom', // <- added this
},

@ljharb i have the same issue.

i have
"react": 15.5.4
"react-dom": 15.5.4
"react-test-renderer": 15.5.4
"enzyme": 2.8.2

It started when i upgraded react-element-to-jsx-string, which no longer has react-addons-test-utils as a dependency, so now it is no longer in my project. it sounds like it was actually required for react 15.5 as well.

i ran the requires in the node repl and they both worked

require('react-test-renderer/shallow')
{ [Function: ReactShallowRenderer] createRenderer: [Function] }

require('react-dom/test-utils')
{ renderIntoDocument: [Function: renderIntoDocument],
..... really long thing

As per @rafaelkallis in my Karma configuration (non-webpack) I added

bundle.external('react-dom/test-utils');
bundle.external('react-test-renderer/shallow');

to my karma.conf.js

This solved the problem for me

@ljharb i just upgraded to the latest webpack and react and enzyme and everything and now when i run unit tests i get the error in my console but they still pass

ERROR in ./node_modules/enzyme/build/react-compat.js
Module not found: Error: Can't resolve 'react-addons-test-utils' in '/Users/.../node_modules/enzyme/build'
@ ./node_modules/enzyme/build/react-compat.js 137:18-52

adding react-addons-test-utils to package.json removes the console error. I already had react-test-renderer. all of them are the latest version in 15.x

I don't think this is relevant any more now that we're on v3.

Was this page helpful?
0 / 5 - 0 ratings