25

I am trying to run the React Example from the Jest React tutorial but I am receiving errors

λ npm test > ... > jest Found 1 matching tests... FAIL __tests__\CheckboxWithLabel-test.js (0.551s) npm ERR! Test failed. See above for more details. npm ERR! not ok code 0 

I have pretty much copied the code directly from the example. The package.json is as follows:

{ "dependencies": { "react": "*", "react-tools": "*" }, "scripts":{ "test": "jest" }, "jest": { "scriptPreprocessor": "<rootDir>/preprocessor.js", "unmockedModulePathPatterns": [ "<rootDir>/node_modules/react" ] }, "devDependencies": { "jest-cli": "~0.1.17" } } 

Any thoughts on what I can do to resolve these errors and run the example test successfully? It's very possible I'm missing an important detail (or details) but not entirely sure what. Oh and for what it's worth, I'm running this on Windows if that impacts this. I would really like to get some tests on my react components (was having some trouble there too so started with the basic examples) -- any help would be appreciated :)

2
  • 1
    I'm seeing the same problem. There is an error that is getting swallowed here which I saw when debugging. On requiring React - ReactCompositeComponent.js: Cannot read property 'DEFINE_MANY' of undefined. It looks like Jest is ignoring the unmockedModulePathPatters in this case. Looks to be related to this github.com/facebook/jest/issues/88 . Perhaps a windows issue? Commented Jul 30, 2014 at 9:08
  • I will create an Ubuntu Vagrant image this evening and try this basic test out there. It does sound very similar to the issue you referenced. Commented Jul 30, 2014 at 13:39

3 Answers 3

27

I created an issue on their github page. Waiting to find out if it is actually a windows related issue

In the meantime, eventually got it working by just specifying the name of the module rather than the relative path

"unmockedModulePathPatterns": ["react"] 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for figuring this out. Also thank you for tossing the issue on the jest issue tracker. I was going to add an issue with the findings unless someone beat me to it :)
5

To anybody who ends up here on a search about this, @ron's github issue was ultimately resolved, and the conclusion was that unmockedModulePathPatterns expects an array of regex statments matching file paths, not necessarily the file paths themselves. This is why using the "relative" paths worked. From the Jest API docs:

An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them. If a module's path matches any of the patterns in this list, it will not be automatically mocked by the module loader.

This is useful for some commonly used 'utility' modules that are almost always used as implementation details almost all the time (like underscore/lo-dash, etc). It's generally a best practice to keep this list as small as possible and always use explicit jest.mock()/jest.dontMock() calls in individual tests. Explicit per-test setup is far easier for other readers of the test to reason about the environment the test will run in.

It is possible to override this setting in individual tests by explicitly calling jest.mock() at the top of the test file.

(https://facebook.github.io/jest/docs/api.html#config-unmockedmodulepathpatterns-array-string)

and from the issue itself:

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

Comments

3

<rootDir> should be replaced with the actual path. It looks like you don't have a subdirectory you want to start from, whereas in some cases you might only want to run tests in a src/ path so your package.json would look more like this:

{ ... "jest": { "rootDir": "src", "scriptPreprocessor": "../jest/preprocessor.js" // Note: relative to src } ... } 

1 Comment

I tried changing the root dir with the actual path, however, it still is not working. Maybe related but I made the test simply work and started adding other code back in. As soon as I get to any of the require statements it fails. I'm basically using the code from github.com/facebook/jest/tree/master/examples/react with the package.json above (and the changes you suggested)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.