unmockedModulePathPatterns can not detect module path #100

Closed
malonecj opened this Issue Jul 30, 2014 · 10 comments

Comments

Projects
None yet
8 participants
@malonecj

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

This comment has been minimized.

Show comment
Hide comment
@billyvg

billyvg 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"]

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

This comment has been minimized.

Show comment
Hide comment
@malonecj

malonecj Jul 31, 2014

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

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

@jeffmo

This comment has been minimized.

Show comment
Hide comment
@jeffmo

jeffmo Aug 4, 2014

Member

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)

Member

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

This comment has been minimized.

Show comment
Hide comment
@malonecj

malonecj 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

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

This comment has been minimized.

Show comment
Hide comment
@dheniges

dheniges Sep 30, 2014

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"
  }

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"
  }

@lionelB lionelB referenced this issue in n1k0/kept Oct 7, 2014

Closed

Enhacement 9 ux for draggable panel #28

@danvk

This comment has been minimized.

Show comment
Hide comment
@danvk

danvk 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)

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

This comment has been minimized.

Show comment
Hide comment
@michalradziwon

michalradziwon Nov 17, 2014

@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...

@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

This comment has been minimized.

Show comment
Hide comment
@mlarcher

mlarcher Mar 23, 2015

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"
    ],

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

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost 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.

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

This comment has been minimized.

Show comment
Hide comment
@cpojer

cpojer Mar 3, 2016

Contributor

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.

Contributor

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.

@cpojer cpojer closed this Mar 3, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment