Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.8.1 fails with "Cannot find module 'react-test-renderer/shallow'" #892

Closed
jwalton opened this issue Apr 12, 2017 · 47 comments · Fixed by #896
Closed

2.8.1 fails with "Cannot find module 'react-test-renderer/shallow'" #892

jwalton opened this issue Apr 12, 2017 · 47 comments · Fixed by #896

Comments

@jwalton
Copy link
Contributor

jwalton commented Apr 12, 2017

Downgrading to 2.8.0 fixes it.

@jwalton
Copy link
Contributor Author

jwalton commented Apr 12, 2017

Read the README. :P

@jwalton jwalton closed this as completed Apr 12, 2017
@pscanf
Copy link

pscanf commented Apr 12, 2017

This is a breaking change, it shouldn't be shipped in a patch release!

@ljharb
Copy link
Member

ljharb commented Apr 12, 2017

@pscanf If you're on React 15.4, it doesn't change anything. If you're on React 15.5, it's a bugfix, because React 15.5 had a breaking change.

@jwalton
Copy link
Contributor Author

jwalton commented Apr 12, 2017 via email

@jwalton jwalton reopened this Apr 12, 2017
@ljharb
Copy link
Member

ljharb commented Apr 12, 2017

Yesterday your module wasn't passing tests because it was upgraded to React 15.5 (due to the semver range), and the new console warnings were failing your tests (assuming you were being a responsible developer and failing your tests on propType warnings).

@jwalton
Copy link
Contributor Author

jwalton commented Apr 12, 2017

I do, in fact, fail on proptype warnings, but not on other warnings. My tests were passing fine. :P

Maybe we could change this bit to something like:

    if (REACT155) {
      try {
          // eslint-disable-next-line import/no-extraneous-dependencies
          shallowRendererFactory = require('react-test-renderer/shallow').createRenderer;
      } catch(err) {
          shallowRendererFactory = TestUtils.createRenderer;
      }
    } else {
      // eslint-disable-next-line import/no-extraneous-dependencies
      shallowRendererFactory = TestUtils.createRenderer;
    }

?

@ljharb
Copy link
Member

ljharb commented Apr 12, 2017

Then there'd be no way to warn users that they're missing a required dependency for React 15.5+.

@jwalton
Copy link
Contributor Author

jwalton commented Apr 12, 2017 via email

@pscanf
Copy link

pscanf commented Apr 12, 2017

Being on [email protected] and [email protected] is ok (this was my situation this morning), while [email protected] + [email protected] fails (my current situation).

and the new console warnings were failing your tests (assuming you were being a responsible developer and failing your tests on propType warnings)

I don't agree, react prints warning specifically not to break things, so enzyme shouldn't break things because anyway react would print warnings.

It might not a big deal I guess (at least for me, since I was updating dependencies anyway), but I would see it as a breaking change nonetheless (which can cause a bit of frustration, sorry for the slightly stormy comment above).

@tomjal
Copy link

tomjal commented Apr 12, 2017

Just adding react-test-renderer dependency fixes everything for me.

@sorahn
Copy link

sorahn commented Apr 12, 2017

@tomjal's fix worked for me.

@ljharb
Copy link
Member

ljharb commented Apr 12, 2017

@pscanf warnings do break things; that's the flawed logic that leads to adding new warnings in a semver-minor.

@lxe
Copy link

lxe commented Apr 13, 2017

Just adding react-test-renderer dependency fixes everything for me.

As much as I love this fix, it's not scalable across the millions of monthly installs enzyme and react get. Fixes should follow semver conventions.

openjck added a commit to mozilla/experiments-viewer that referenced this issue Apr 13, 2017
See this discussion about the enzyme change that caused tests to fail:
enzymejs/enzyme#892
@ljharb
Copy link
Member

ljharb commented Apr 13, 2017

@lxe they did. React 15.5 shipped a change that was subjectively either breaking or minor, which broke some subset of users and libs. enzyme had two choices here: ship a patch that fixes things for those that React 15.5 broke (causing breakage for those users who weren't broken with React 15.5); or ship a semver-major and leave users on the current major line screwed, and forced to adapt to the React breakage (and thus accommodating those users who weren't broken with React 15.5).

semver does not strictly dictate whether "console warnings" are semver-major or not, as such, considering them breaking or non-breaking are both a valid choice.

@openjck
Copy link

openjck commented Apr 13, 2017

Without taking sides, I'll say that the semver folks would probably be happy to discuss any ambiguities in a GitHub issue and update their guidelines if necessary.

@ljharb
Copy link
Member

ljharb commented Apr 13, 2017

@openjck the issue is on what constitutes "API" - the document says "For this system to work, you first need to declare a public API. This may consist of documentation or be enforced by the code itself." however I claim that the API consists of "anything that's programmatically observable". Additionally, that document does talk about deprecations being in documentation; React adds deprecations in a semver-minor observably in the code, which I believe constitutes an API change.

@jwalton
Copy link
Contributor Author

jwalton commented Apr 13, 2017

React 15.5 shipped a change that was subjectively either breaking or minor, which broke some subset of users and libs.. enzyme had two choices here: ship a patch that fixes things for those that React 15.5 broke (causing breakage for those users who weren't broken with React 15.5); or ship a semver-major and leave users on the current major line screwed, and forced to adapt to the React breakage (and thus accommodating those users who weren't broken with React 15.5).

React 15.5 shipped a change that was objectively a minor change. It adds a console.log, which isn't even present in production builds. It affects absolutely nothing for my users.

Enzyme had a third choice; do exactly the fix I proposed above. With that fix, if you had 15.5 but hadn't gotten around to doing all the 15.5 fixes and things were working, Enzyme would have had a minor patch and things would have kept working. If you had 15.5 and you were a "responsible developer" whose tests break for any console logs, then when Enzyme referenced TestUtils.createRenderer, React would have logged the warning, and tests would have broken, but it would have been React breaking them, not Enzyme. If you had React 15.5 and you did all the 15.5 upgrades, then Enzyme would have happily chugged along with no warnings, and all would be well. Enzyme would work correctly in all cases, which is the definition of correct software.

I'm not sure why a major version would leave anyone "screwed" - it would have let people know they needed to do something when david told them there was a package that needed upgrading. I'd much rather get a notification from david that I need to do something some time next week than a wall of red from travis-ci for all my various projects on the day before my code freeze.

@tiriana
Copy link

tiriana commented Apr 13, 2017

I'm using react ^0.14.8 (I know - old) and it also affected me. I downgraded enzyme to ~2.7.1 and things got back to normal.

I'm not so much up to date with news in react, but imo if external libs, depending on enzyme, work with enzyme 2.8.0 (@jwalton proposed downgrading to this version as a solution in first comment) and not 2.8.1 then it sounds like breaking changes for me. Not a patch.

I just hope I'll be able to use enzyme in future versions, b/c I won't be able to update my react any time soon unfortunately.

@tiriana
Copy link

tiriana commented Apr 13, 2017

@jwalton adding react-test-renderer didn't work for me - I got another error: Cannot find module 'react-dom/test-utils'. Just FYI.

I mentioned @tomjal in prev version of my comment by mistake - I have no idea how autocomplete lead me to do this :-)

@shahzainb
Copy link

Just to re-iterate from @tiriana's point, we are also using React 0.14.8 and enzyme ^2.2.0. Everything was working fine until enzyme 2.8.1 shipped yesterday. Our react version didn't change. Downgrading and fixing version to 2.8.0 solved our problem too.

@jwalton
Copy link
Contributor Author

jwalton commented Apr 13, 2017

0.14.8 works for me. If you open a new folder and try:

npm install react@^0.14.8 react-dom@^0.14.8 react-addons-test-utils@^0.14.8 [email protected]
node -e "try{require('enzyme'); console.log('pass');} catch(err) {console.log('***FAIL***', err.stack);}"

Does that print "pass" or "fail"? Is it possible you have some dependency that's pulling in react 0.15.5 and causing this? You can npm ls to see if react 15.5 is in there anywhere.

@shahzainb
Copy link

So that prints pass, but when running the unit tests through karma, I get this:

13 04 2017 16:56:10.341:ERROR [framework.browserify]: bundle error
13 04 2017 16:56:10.346:ERROR [framework.browserify]: Error: Cannot find module 'react-dom/test-utils' from '<redacted>/node_modules/enzyme/build'
13 04 2017 16:56:10.365:INFO [karma]: Karma v0.13.22 server started at http://localhost:9876/
13 04 2017 16:56:10.373:INFO [launcher]: Starting browser PhantomJS
13 04 2017 16:56:10.430:ERROR [framework.browserify]: bundle error
13 04 2017 16:56:10.430:ERROR [framework.browserify]: Error: Cannot find module 'react-test-renderer/shallow' from '<redacted>/node_modules/enzyme/build'

npm ls | grep react shows me this:

├── @kadira/[email protected] extraneous
├─┬ [email protected]
│ ├─┬ [email protected]
│ ├─┬ [email protected]
│ │ ├─┬ [email protected]
│ ├─┬ [email protected]
│ ├─┬ [email protected]
│ └─┬ [email protected]
├─┬ [email protected]
npm ├─┬ [email protected]
ERR!├── [email protected]
├── [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ ├── [email protected]
├─┬ [email protected]

@ljharb
Copy link
Member

ljharb commented Apr 13, 2017

If it's affecting users without React 15.5 in their trees, then it is definitely a bug that needs fixing, but I'm not sure how it could be.

@jwalton
Copy link
Contributor Author

jwalton commented Apr 13, 2017

That is definitely a mystery. This is a long shot but, @shahzainb do you have any projects npm linked into your tree?

The only place enzyme tries to require 'react-test-renderer' is here, and it's guarded by the REACT155 check which is set here in a pretty non-ambiguous fashion. It's got to be picking up the new react from somewhere... I'm pretty sure my PR would fix this, but only because it's going to try to fall back on the old code.

@azeldis-lv
Copy link

The enzyme 2.8.1 upgrade also mysteriously broke builds for me as well (we are running react 15.3.2). I am pinning to 2.8.0

@ljharb
Copy link
Member

ljharb commented Apr 14, 2017

v2.8.2 published. Please comment if you're still having trouble.

@tiriana
Copy link

tiriana commented Apr 14, 2017

@jwalton About the snippet you pasted - it logged pass in my case.

@tiriana
Copy link

tiriana commented Apr 14, 2017

@ljharb Thx for quick reaction. But unfortunately I still get this issue: Cannot find module 'react-test-renderer/shallow'.

@ljharb
Copy link
Member

ljharb commented Apr 14, 2017

@tiriana can you file a new issue, and include your installed versions of react (and react-related things)?

@tiriana
Copy link

tiriana commented Apr 14, 2017

@jwalton I actually have react 0.14.9, but nothing seems to pull react 15.5

npm ls | grep react
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
├─┬ [email protected]
├─┬ [email protected]
├── [email protected]
├── [email protected]

@tiriana
Copy link

tiriana commented Apr 14, 2017

@ljharb sure thing, will do.

@tiriana
Copy link

tiriana commented Apr 14, 2017

@ljharb done: #899

@jsg2021
Copy link

jsg2021 commented Apr 14, 2017

Sorry, but 2.8.2 still fails

Cannot find module 'react-addons-test-utils' from 'react-compat.js'
      
      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:169:17)
      at node_modules/enzyme/build/react-compat.js:143:21
      at Object.<anonymous> (node_modules/enzyme/build/react-compat.js:214:5)
npm ls | grep react
├─┬ [email protected]
│ ├─┬ [email protected]
│ ├─┬ [email protected]
│ │ ├─┬ [email protected]
│ ├─┬ [email protected]
│ ├─┬ [email protected]
│ └─┬ [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
├─┬ [email protected]
├─┬ [email protected]
├─┬ [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ ├── [email protected]
├─┬ [email protected]
│ ├── [email protected] deduped

@azeldis-lv
Copy link

Still failing my build too, with react 15.3.2

@jsg2021
Copy link

jsg2021 commented Apr 14, 2017

if I add npm i react-test-renderer it passes. Does enzyme not list this dependency? Or is that the consumer's responsibility? How would you conditionally depend on one or the other? Seems like a case for a major version bump of enzyme?

@jsg2021
Copy link

jsg2021 commented Apr 14, 2017

I'm trying to find where react-addons-test-utils used to come from... to find where react-test-renderer should go :/

@ljharb
Copy link
Member

ljharb commented Apr 14, 2017

See the readme; you need to explicitly add it.

@jsg2021
Copy link

jsg2021 commented Apr 14, 2017

oh :) sorry

@paibamboo
Copy link

paibamboo commented Apr 18, 2017

Upgrading enzyme from 2.8.0 to 2.8.1 or 2.8.2 without upgrading React breaks my build, too.

The error is
Cannot find module 'react-dom/test-utils'

I'm using react 15.4.x. and react-dom 15.4.x


Ok, I figured out, you need react-dom 15.5.x for react-dom/test-utils

@aweary
Copy link
Collaborator

aweary commented Apr 18, 2017

@paibamboo it shouldn't attempt to require react-dom/test-utils unless you're using React >=15.5.

@GreenGremlin
Copy link

Why is react-test-renderer not declared as as peerDependency? I understand it's only required for [email protected], but I don't see the problem with declaring it.

@ljharb
Copy link
Member

ljharb commented May 1, 2017

Declaring it forces everyone to install it, even if they're not using v15.5.

@ZebraFlesh
Copy link

ZebraFlesh commented May 6, 2017

2.8.2 still fails for me where 2.8.0 worked. I'm using react, react-dom, and react-addons-test-utils all versioned at 15.4.2; karma 1.7.0, and webpack 2.1. The way I finally made this work was to extend my webpack externals object to be the following:

externals: {
    'react/addons': true,                      // pre-existing at enzyme 2.8.0
    'react/lib/ExecutionEnvironment': true,    // pre-existing at enzyme 2.8.0
    'react/lib/ReactContext': true,            // pre-existing at enzyme 2.8.0
    'react-dom/test-utils': true,
    'react-test-renderer/shallow': true
}

I tried a few different permutations with react-test-renderer before settling on this solution:

  1. install react-test-renderer, no external -> failed
  2. install react-test-renderer, with external -> passed
  3. uninstall react-test-renderer, with external -> passed
  4. uninstall react-test-renderer, no external -> failed

So clearly listing the package as an external for webpack was sufficient. All of my shallow rendering tests still pass, and even adding a failing expectation such as expect(wrapper).to.have.className('thisDoesNotExist'); fails as expected, so it seems to be working in this configuration?

@noahehall
Copy link

@ZebraFlesh solution worked for me

@ljharb
Copy link
Member

ljharb commented May 9, 2017

Sounds like we may need to update the webpack documentation, and then we can close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.