I'm trying to run my test suite but I am getting the following error:
Couldn't find preset "module:metro-react-native-babel-preset" relative to directory "/Users/dan/Sites/X"
at node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
at Array.map (<anonymous>)
at OptionManager.resolvePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
at OptionManager.mergePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
at OptionManager.mergeOptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
at OptionManager.init (node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
at File.initOptions (node_modules/babel-core/lib/transformation/file/index.js:212:65)
at File.initOptions (node_modules/ts-jest/dist/util/hacks.js:23:43)
at new File (node_modules/babel-core/lib/transformation/file/index.js:135:24)
at Pipeline.transform (node_modules/babel-core/lib/transformation/pipeline.js:46:16)
Here's my jest
config in package.json
:
"jest": {
"preset": "react-native",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json"
],
"transform": {
"^.+\\.(js)$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",
"\\.(ts|ts)x?$": "ts-jest"
},
"testRegex": "/src/.*.spec.(js|ts|tsx)?$",
"globals": {
"ts-jest": {
"useBabelrc": true
}
}
}
If needed, here's the contents of babelrc
{
"presets": ["module:metro-react-native-babel-preset"],
"plugins": [
[
"module-resolver",
{
"alias": {
"~": "./src"
}
}
]
]
}
This issue remains open at the time of writing.
Users seem to have mixed results with the fixes, but these two worked for me:
Simply rename .babelrc
to .babelrc.js
or babel.config.js
.
Don't forget to add module.exports =
to the beginning of the file.
This fixed my jest issue but broke other eslint plugins for me.
For option 2, I think it works around the issue by manually invoking the react-native preprocessor on all javascript files.
transform: { '^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js' }
If you don't have a separate Jest config file, you can add it to your package.json like...
{
"jest": {
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With