Jest Ignore react peer dependency in linked module
Asked Answered
K

1

7

I'm working on a React web app that uses a component library we include as an npm module. Because I'm working on the library and the webapp at the same time, I linked the library as module in npm.

The directories are set up like this:

~/Development/
|
|-- myWebapp/
|   |-- package.json # (react included here as dependency)
|   |-- src/
|   |-- node_modules/
|       |-- jest-cli/
|       |-- react/
|       |-- myLibrary/ -> /usr/local/lib/node_modules/myLibrary
|
|-- myLibrary/       # (linked to /usr/local/lib/node_modules by npm link)
|   |-- package.json
|   |-- src/
|   |-- node_modules/
|       |-- react/   # (included as a devDependency and peerDependency)

I'm writing my tests in Jest (0.8.2).

I'm trying to configure Jest to not load React from myLibrary/node_modules/react when I run tests for myWebapp. Loading React twice tends to cause problems. My tests pass if I rm -r myLibrary/node_modules/react before I run them, but it's a pain to uninstall and reinstall React all the time.

I tried to configure Jest with:

...,
"modulePathIgnorePatterns": [ 
    "<rootDir>/node_modules/myLibrary/node_modules/react"
],
... 

but that did not seem to work

Kurtzig answered 28/9, 2016 at 18:56 Comment(3)
Did you get any solution? I just raised an issue on Jest's github repo: github.com/facebook/jest/issues/2447Synthiasyntonic
@olvarra1 Not a great one, but there is a solution. We ended up including a 'built' version of myLibary in a folder within the project structure called dist/. The solution is to only link dist/ within myWebapp/node_modules/myLibrary/ We published the module myLibary to a private npm registry. After that, we installed myLibary within myWebapp as a normal dependency, but went into myWebapps/node_modules/myLibrary, deleted the dist/ directory, and the linked dist/ from the local working copy of myLibrary like ln -s ../../../myLibrary/dist dist/Kurtzig
I'm hitting this issue myself and am completely blocked by it. I've spent hours on google trying to find an answer. I've tried many solutions but nothing works. So I'm curious, did you ever find a final solution to this?Busily
Y
0

Try a new regex for modulePathIgnorePatterns:

"modulePathIgnorePatterns": [ 
    "<rootDir>/node_modules.*/react"
]

.* should match any character zero or more times. So, all react should be excluded.

Ywis answered 29/9, 2016 at 2:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.