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

require traversing wrong directory to find package #609

Closed
hgezim opened this issue Nov 13, 2015 · 13 comments
Closed

require traversing wrong directory to find package #609

hgezim opened this issue Nov 13, 2015 · 13 comments

Comments

@hgezim
Copy link
Contributor

hgezim commented Nov 13, 2015

Amongst other packages, react-bootstrap depends on:

  • react-prop-types@0.3.0
  • react-overlays which depends on:
    • react-prop-types@0.2.2

npm 3 installs react-prop-types as follows:

$ find . -name 'react-prop-types'
./node_modules/react-overlays/node_modules/react-prop-types # happens to be v0.2.2
./node_modules/react-prop-types #happens to be v0.3.0

The problem rises in node_modules/react-bootstrap/lib/ButtonGroup.js where require is called as follows:

var _reactPropTypesLibAll = require('react-prop-types/lib/all');

Since react-bootstrap depends on react-prop-types@0.3.0, it should find ./node_modules/react-prop-types but instead it finds ./node_modules/react-overlays/node_modules/react-prop-types and then fails with:

Runtime Error
Error: /home/ubuntu/frontend/src/javascript/components/MyComponentSelector/__tests__/MyComponentListItem-test.js: /home/ubuntu/frontend/src/javascript/components/MyComponentSelector/MyComponentListItem.jsx: /home/ubuntu/frontend/node_modules/react-bootstrap/lib/index.js: /home/ubuntu/frontend/node_modules/react-bootstrap/lib/ButtonGroup.js: Invalid argument must be an array
    at Object.all [as default] (/home/ubuntu/frontend/node_modules/react-overlays/node_modules/react-prop-types/lib/all.js:12:11)
    at Object.<anonymous> (/home/ubuntu/frontend/node_modules/react-bootstrap/lib/ButtonGroup.js:37:45)
    at Object.runContentWithLocalBindings (/home/ubuntu/frontend/node_modules/jest-cli/src/lib/utils.js:397:17)
    at Loader._execModule (/home/ubuntu/frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:234:11)
    at Loader.requireModule (/home/ubuntu/frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:845:14)
    at Loader.requireModuleOrMock (/home/ubuntu/frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:875:19)
    at Object.<anonymous> (/home/ubuntu/frontend/node_modules/react-bootstrap/lib/index.js:75:21)
    at Object.runContentWithLocalBindings (/home/ubuntu/frontend/node_modules/jest-cli/src/lib/utils.js:397:17)
    at Loader._execModule (/home/ubuntu/frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:234:11)
    at Loader.requireModule (/home/ubuntu/frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:845:14)
    at Loader.requireModuleOrMock (/home/ubuntu/frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:875:19)
    at Object.<anonymous> (/home/ubuntu/frontend/src/javascript/components/MyComponentSelector/MyComponentListItem.jsx:2:1)
    at Object.runContentWithLocalBindings (/home/ubuntu/frontend/node_modules/jest-cli/src/lib/utils.js:397:17)
    at Loader._execModule (/home/ubuntu/frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:234:11)
    at Loader.requireModule (/home/ubuntu/frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:845:14)
    at Loader.requireModuleOrMock (/home/ubuntu/frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:875:19)
    at Object.<anonymous> (/home/ubuntu/frontend/src/javascript/components/MyComponentSelector/__tests__/MyComponentListItem-test.js:9:25)
    at Object.runContentWithLocalBindings (/home/ubuntu/frontend/node_modules/jest-cli/src/lib/utils.js:397:17)
    at Loader._execModule (/home/ubuntu/frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:234:11)
    at Loader.requireModule (/home/ubuntu/frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:845:14)
    at jasmineTestRunner (/home/ubuntu/frontend/node_modules/jest-cli/src/jasmineTestRunner/jasmineTestRunner.js:264:16)
    at /home/ubuntu/frontend/node_modules/jest-cli/src/TestRunner.js:379:14
    at process._tickCallback (node.js:377:9)

This fails when running tests with jest. It doesn't look like a mocking issue.

To be clear, my project doesn't directly require react-prop-types:

$ grep 'react-prop-types' package.json 
$ grep 'react' package.json 
    "react": "0.14.2",
    "react-addons-pure-render-mixin": "0.14.2",
    "react-addons-transition-group": "0.14.2",
    "react-addons-update": "0.14.2",
    "react-bootstrap": "0.27.3",
    "react-dom": "0.14.2",
    "react-router": "1.0.0-rc4",
    "babel-preset-react": "6.0.15",
    "react-addons-perf": "0.14.2",
    "react-addons-test-utils": "0.14.2",
$
@clentfort
Copy link
Contributor

Have you, during your project livetime, upgraded from npm2 to npm3? It seems that the dependency is installed twice.
Could you try cleaning node_modules and running npm install again?

@hgezim
Copy link
Contributor Author

hgezim commented Nov 13, 2015

Yes, I was using npm2 before upgrading to npm3 but have definitely removed node_modules and then ran npm install.

What makes you say [i]t seems that the dependency is installed twice?

@cpojer
Copy link
Member

cpojer commented Nov 13, 2015

This seems related to #554.

Can you add react-overlays and react-prop-types to your unmocked module list in the config?

@hgezim
Copy link
Contributor Author

hgezim commented Nov 13, 2015

@cpojer, it's not. No mocking happening at all for that test. Also tried adding the two packages to unmocked list.

@cpojer
Copy link
Member

cpojer commented Nov 13, 2015

Oh I'm sorry, I see now what you mean. The problem is that two projects depend on the same dependency but different versions of it. npm3 installs one of them in node_modules and the other one in node_modules/react-overlays/node_modules and the module loader seems to pick the wrong one first, is that right?

@amasad does the new resolver fix this use-case? What does it give preference to?
@hgezim did the module resolution algorithm change at all with npm3?

@hgezim
Copy link
Contributor Author

hgezim commented Nov 13, 2015

That's correct, @cpojer.

I scoured the npm changelog and only found this but doubt it's related at all: npm/npm@a8c8a13 .

What handles resolution by require? I haven't got a clue!

@cpojer
Copy link
Member

cpojer commented Nov 18, 2015

I think this won't be a problem with the new resolver from #599. I'll close it once this gets merged into jest at the beginning of December.

@cpojer
Copy link
Member

cpojer commented Feb 17, 2016

@hgezim would you mind retrying with jest@next (0.9.0-fb1) and reporting whether it resolves your issue?

@hgezim
Copy link
Contributor Author

hgezim commented Feb 17, 2016

Hi @cpojer, can't confirm if it's fixed. Getting an error for my use of JSX in the test file. Probably babel-jest is not compatible with latest jest?!

npm test -- src/javascript/components/Scheduler/__tests__/TimeSelector-test.js

> acme-frontend@0.0.1 test /Users/gezim/projects/acme/frontend
> jest --maxWorkers 4 "src/javascript/components/Scheduler/__tests__/TimeSelector-test.js"

Using Jest CLI v0.9.0-fb1, jasmine1
 FAIL  src/javascript/components/Scheduler/__tests__/TimeSelector-test.js
● Runtime Error
SyntaxError: Unexpected token <
    at eval (native)
    at Object.<anonymous> (/Users/gezim/projects/acme/frontend/src/javascript/components/Scheduler/__tests__/TimeSelector-test.js:9:20)
    at process._tickCallback (node.js:402:9)

npm ERR! Test failed.  See above for more details.

@cpojer
Copy link
Member

cpojer commented Feb 18, 2016

That seems unrelated to the 0.9 update and is likely also happening on 0.8 then? You can also try --no-cache.

@cpojer
Copy link
Member

cpojer commented Mar 3, 2016

Please let me know if this is still a problem with jest-cli@next and I will reopen and investigate.

@cpojer cpojer closed this as completed Mar 3, 2016
@hgezim
Copy link
Contributor Author

hgezim commented Apr 11, 2016

Fixed! Thanks so much! Sorry I couldn't test it earlier.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants