Enzyme: Cannot find module 'react' from 'ReactSixteenAdapter.js'

Created on 24 Dec 2018  路  8Comments  路  Source: enzymejs/enzyme

Hi, I have some issue in my project about react high order component. I think it's because react moved in peerDependencies.

Cannot find module 'react' from 'ReactSixteenAdapter.js'
      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
      at Object.<anonymous> (node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js:15:14)

More logs:
https://travis-ci.com/Luchanso/react-has-mounted/builds/95682075#L456-L462
Pull Request with changes.

Version

| library | version
| ------------------- | -------
| enzyme | ^3.8.0
| react | ^16.7.0
| react-dom | ^16.7.0
| react-test-renderer | -
| adapter (below) | ^1.7.1

Adapter

  • [x] enzyme-adapter-react-16

Most helpful comment

In that case, your package must have react as both a peer dep and a dev dep - the dev dep for testing, the peer dep to communicate compatibility to consumers.

All 8 comments

It being a peer dependency means you have to explicitly have it as a dep or dev dep on the top level. Have you?

Note that everything that鈥檚 not the top level app should have it as a peer dep (and dev dep) and not as a regular dep.

I have component as a package and I want to test it, can you suggest best way how to be?

In that case, your package must have react as both a peer dep and a dev dep - the dev dep for testing, the peer dep to communicate compatibility to consumers.

@ljharb okey, it's work, thank you for help 馃憤

Hi @ljharb ,

I ran into the same issue with react-native. My test environment includes jest, enzyme, I also implemented typescript.

I have it in my dep an devdep

```"dependencies": {
"@types/react-test-renderer": "^16.8.1",
"react": "16.3.1",
"react-native": "0.55.4",
"react-native-iap": "^2.4.0-beta5",
"react-native-video": "^3.2.0"
},
"devDependencies": {
"@types/enzyme": "^3.9.1",
"@types/enzyme-adapter-react-16": "^1.0.5",
"@types/he": "1.1.0",
"@types/jest": "^24.0.12",
"@types/react": "^16.8.17",
"@types/react-native": "^0.57.53",
.....
}

but I still go the error:

Cannot find module 'react-dom' from 'ReactSixteenAdapter.js'

at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
at Object. (node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:3:1)
```

Any idea why?

enzyme-adapter-react-16 is explicitly meant to work with react-dom, so you must have that installed.

To use RN, you'd need an actual RN adapter - see #1436 for that.

Hi, I am trying to get Jest and Enzyme setup for the first time. I have added them in my dev dependencies, but still see the error Cannot find module 'react' from 'ReactSixteenAdapter.js'
"devDependencies": { "enzyme": "^3.11.0", "jest-cli": "^20.0.4", "react": "^16.13.1", "react-dom": "^16.13.1" },

Any help would be great. Thanks in advance.

@stuthib please file a new issue, and fill out the issue template, and i'll be happy to take a look.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ivanbtrujillo picture ivanbtrujillo  路  3Comments

dschinkel picture dschinkel  路  3Comments

blainekasten picture blainekasten  路  3Comments

blainekasten picture blainekasten  路  3Comments

rexonms picture rexonms  路  3Comments