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

unmockedModulePathPatterns can not detect module path #100

Closed
malonecj opened this issue Jul 30, 2014 · 11 comments
Closed

unmockedModulePathPatterns can not detect module path #100

malonecj opened this issue Jul 30, 2014 · 11 comments

Comments

@malonecj
Copy link

Think this might be related to closed issue #88

I'm using a windows machine. Any time I try to require an external module, be it in node_modules, or any folder relative to the spec file, I receive an npm error

Found 1 matching tests...
FAIL tests\xxx.spec.js
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0

There was no more debug information available so I reduced my spec file to a single line
var React = require('react'); and found that it was failing when trying to require react module.

My package.json file seems to be in order at least according to the example in the docs.
Here's a section of it

"dependencies": {
    "react": "^0.10.0",
    "react-tools": "^0.11.0"
  },
  "scripts": {
    "test": "jest"
  },
  "jest": {
    "testFileExtensions" : ["js"],
    "unmockedModulePathPatterns": ["./node_modules/react/"]
  }

My directory structure looks something like this so the <rootDir> should default to where package.json is. And it should be able to find react library from unmockedModulePathPatterns provided

+-- __tests__
|   +-- testfile.spec.js
+-- node_modules
|   +-- react
+-- testfile.js
+-- package.json
+-- preprocessor.js

I debugged the source code and found that there is an error that is getting swallowed on requiring React at node_modules\lib\ReactCompositeComponent.js: Cannot read property 'DEFINE_MANY' of undefined - which would suggest to me that Jest is ignoring the unmockedModulePathPatterns

@billyvg
Copy link

billyvg commented Jul 30, 2014

I'm getting the same thing, except I'm on OSX. My setup is a little difference since I'm redefining rootDir.

"jest": {
      "rootDir": "./app/web/modules",
      "moduleFileExtensions": ["js", "jsx"],
      "scriptPreprocessor": "../../../preprocessor.js",
      "unmockedModulePathPatterns": ["../../../node_modules/react"]
}

Here is my directory structure

+-- node_modules/
|    +-- react/
+-- app/
|    +-- web/
|          +-- modules/
|               +-- __tests__/
+-- package.json
+-- preprocessor.js

The path to preprocessor is correct and runs the preprocessor script fine.

Edit I added "react" to the unmockedModulePathPatterns array and it seems to be including fine now...

"unmockedModulePathPatterns": ["react"]

@malonecj
Copy link
Author

yes, setting "unmockedModulePathPatterns": ["react"] instead of the relative path works.
Now I can get started at least, cheers

@jeffmo
Copy link
Contributor

jeffmo commented Aug 4, 2014

Interesting, I wonder if there's a normalization problem when rootDir isn't left at the default...

I'm coming back from a pretty long vacation and I have quite a few issues to slog through, so it may be a bit of time before I can dive into this myself. However, if you want to dive in I'd be happy take answer questions or take any PRs in the meantime :)

Pro-tip: If you run jest --runInBand, jest will execute all within a single process...making it much easier to debug (i.e. using something like node-inspector)

@malonecj
Copy link
Author

malonecj commented Aug 5, 2014

I'll try and take a look at this one so. I'm not having a lot of luck with node-inspector though. It's quite temperamental, only occasionally loading the jest scripts in the debugger window. I have tried a few ways node-debug jest --runInBand from the <rootDir> as well as starting up the inspector first eg

$ node-inspector
$ node --debug-brk .\node_modules\jest-cli --runInBand

Am I doing something wrong here? Sorry, don't have much experience with node-inspector. Just going from the docs

@dheniges
Copy link

I'm having the same issue, and changing the unmockedModulePathPatterns to react instead of a relative path fixed it. My package.json for jest (with non-relative path):

"jest": {
    "scriptPreprocessor": "<rootDir>/tests/preprocessor.js",
    "unmockedModulePathPatterns": [
      "react"
    ],
    "testDirectoryName": "tests"
  }

@danvk
Copy link

danvk commented Oct 17, 2014

I'm seeing this error sporadically in our Travis build. I've seen trivial changes make this error appear & disappear, so I assume it's flakiness. Examples:

https://travis-ci.org/hammerlab/cycledash/builds/38193754 (failed)
https://travis-ci.org/hammerlab/cycledash/builds/38199591 (passed: added a "cat" statement)
https://travis-ci.org/hammerlab/cycledash/builds/38200941 (passed: removed "cat")
https://travis-ci.org/hammerlab/cycledash/builds/38206214 (failed: merged PR)

@michalradziwon
Copy link

@dheniges thanks. After removing relative path it started working on my Windows 7.
On Amazon Linux 2014 and Ubuntu 14 it works without changing the path...

@mlarcher
Copy link

unmockedModulePathPatterns are used internally by Jest to create a RegExp against which all required modules will be tested. As such, you need to provide a valid regex pattern.
For example, this worked nicely for me :

unmockedModulePathPatterns: [
      "node_modules\\" + path.sep + "react",
      "node_modules\\" + path.sep + "reflux",
      "node_modules\\" + path.sep + "react-router"
    ],

@ghost
Copy link

ghost commented Aug 4, 2015

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

@cpojer
Copy link
Member

cpojer commented Mar 3, 2016

unmockedModulePathPatterns will only work with absolute paths or partial path patterns, but no relative paths. This is by design and will most likely not be changed.

@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

8 participants