Jest preset and Yarn Workspaces
See original GitHub issueIs this a bug report?
Yes, or maybe incompatibility with a Yarn feature is more accurate
Have you read the Contributing Guidelines?
Yes
Environment
Environment: OS: macOS Sierra 10.12.6 Node: 8.9.1 Yarn: 1.3.2 npm: 5.5.1 Watchman: 4.7.0 Xcode: Xcode 9.2 Build version 9C40b Android Studio: 2.2 AI-145.3360264
Packages: (wanted => installed) react: 16.0.0 react-native: 0.51.0
Target Platform: N/A
Steps to Reproduce
react-native init rnproject
rm -rf rnproject/node_modules
- Add
package.json
:
{
"private": true,
"workspaces": [
"rnproject"
]
}
yarn
cd rnproject
yarn test
● Validation Error:
Module <rootDir>/node_modules/react-native/jest/assetFileTransformer.js in the transform option was not found.
Configuration Documentation:
https://facebook.github.io/jest/docs/configuration.html
error Command failed with exit code 1.
Expected Behavior
React Native’s jest preset should work with projects using yarn workspaces, where modules may not be in the project’s node_modules
directory (they may be hoisted in the parent dir’s node_modules
.)
Actual Behavior
The jest preset provided by React Native specifies <rootDir>/node_modules/...
in a couple places, which is not always the location of certain modules (i.e. when using Yarn workspaces):
"modulePathIgnorePatterns": [
"<rootDir>/node_modules/react-native/Libraries/react-native/"
],
"transform": {
"^...$": "<rootDir>/node_modules/react-native/jest/assetFileTransformer.js"
},
"setupFiles": [
"<rootDir>/node_modules/react-native/jest/setup.js"
],
Thus, Jest produces an error saying that it can’t find the files in the right spots.
The Yarn documentation on workspaces is opinionated on making assumptions about the package layout, I tend to agree:
The package layout will be different between your workspace and what your users will get (the workspace dependencies will be hoisted higher into the filesystem hierarchy). Making assumptions about this layout was already hazardous since the hoisting process is not standardized, so theoretically nothing new here.
But I’m also not sure how to achieve a layout-agnostic kind of a setup.
Reproducible Demo
https://github.com/dingbat/ws-rn-bug-example
Some fixes I tried
- When running jest, specifying the root dir as the workspace root (
--rootDir ..
). This caused a bunch more errors and didn’t seem like a good direction, because we should be able to run tests isolated to a subdir anyway. - Copy and paste the RN config in with the paths modified for the hoisted
node_modules
. Tried this with and without"preset": "react-native"
, neither one worked- Using the preset still tried loading in a setup file from the RN preset (I think because it specifies an array)
Module <rootDir>/node_modules/react-native/jest/setup.js in the setupFiles option was not found.
- Not using the preset probably will never work…
Cannot find module 'setupDevtools' from 'setup.js'
- Using the preset still tried loading in a setup file from the RN preset (I think because it specifies an array)
- Symlink
rnproject/node_modules/react-native
tonode_modules/react-native
(not a real solve since this wouldn’t survive Git and would have to be some sort of build step). No luck, for some reason Jest still can’t find the asset transformer:● Validation Error: Module <rootDir>/node_modules/react-native/jest/assetFileTransformer.js in the transform option was not found. Configuration Documentation: https://facebook.github.io/jest/docs/configuration.html
Note
My intuition tells me there’s no easy fix in this repo, and there’s much better luck of addressing it in the Jest repo. But since the broken jest-preset configuration is provided in this repo, I thought it would make more sense to open an issue here.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:30
- Comments:24
Top GitHub Comments
This is a bit of a nightmare, if Yarn supported per-project nohoist then there would be an easy option, but instead I had to blanket nohoist both
react-native
andjest
and all their dependencies (which as you can imagine is an awful lot of dependencies) to get around this. Latest version of react-native (0.55.1). Not sure why this issue is closed as it is definitely an incompatibility with a widely used feature of Yarn?This worked for me: https://github.com/justinsisley/Jest-CSS-Modules/issues/13#issuecomment-368233843