metro: Couldn't find preset "module:metro-react-native-babel-preset" when running jest
Do you want to request a feature or report a bug? bugreport
What is the current behavior?
I updated a react-native codebase from 0.56 to 0.57 and migrated from "babel-preset-react-native": "^2.1.0" to the current version of "metro-react-native-babel-preset". The app itself compiles and runs for iOS and android but all of the jest tests crash immediately on my mac and on our linux ci.
Test suite failed to run
Couldn't find preset "module:metro-react-native-babel-preset" relative to directory "/Users/michaelknoch/dev/repos/JestCrashReproduction"
If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test.
- clone https://github.com/michaelknoch/jest-metro-reproduction
- npm i
- npm run test
I created this repo with react-native init. To run Jest we need at leat one file with pattern *.test.js
So i ended up running this: react-native init JestMetroCrash --version 0.57.0 && cd JestMetroCrash && touch App.test.js
Its worth to mention that the test suite also breaks when .babelrc with explicit "presets": ["module:metro-react-native-babel-preset"] is provided.
What is the expected behavior? Jest should resolve the preset and transpile files correctly.
Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system. node v8.11.3 npm 6.4.1 metro latest release
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 51
- Comments: 46 (2 by maintainers)
Commits related to this issue
- :bug: Add Jest path fix from StackOverflow https://github.com/facebook/metro/issues/242#issuecomment-421139247 — committed to dashevo/dashpay-wallet-OLD by seigler 6 years ago
- Added babel-core@bridge and cleaned up Jest config per comments in https://github.com/facebook/metro/issues/242#issuecomment-431350665 — committed to blefebvre/react-native-sqlite-demo by blefebvre 6 years ago
- downgrade react version as it is broken newer version of react is broken, jest doesn't work at all solution worked: https://github.com/react-community/create-react-native-app/issues/726#issuecomment... — committed to yurykabanov/helloworld-rn by yurykabanov 6 years ago
I have found the same problem today. Open the file
.babelrc, you will find it in the root of the project, and replace:{ "presets": ["module:metro-react-native-babel-preset"] }to
{ "presets": ["react-native"] }For your jest configuration, can you also add
transform: { '^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js' }?I confirm that @ajcrites’s solution works, event with TypeScript. My
jestconfig inpackage.jsonis following:After I added the custom transform mocks stopped working as well. These are the steps I completed to fix it.
'babel-core@^7.0.0-bridge'transform: { '^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js' }).babelrctobabel.config.jsbabel.config.js
now my tests are running and mocks are working as usual 😃 hope it can help somebody
inside my react-native-vector-icons folder, in node_modules, there was a bablerc file which was like this
I manually changed it to this :
still running into
Couldn't find preset "module:metro-react-native-babel-preset"with 0.47.0I hope this can help
"presets": ["module:metro-react-native-babel-preset"]"presets": ["react-native"]trims
Replacing with
{ "presets": ["react-native"] }can’t be the solution for the existing set of tests, as it breaks loading babel-jest and fails to mock of files that test import using ES6.Hey @michaelknoch. I have the following versions:
And it works just fine. Just had to change my
.babelrctobabelrc.jsand installbabel-core(npm i -D babel-core@bridge).the solution from @ajcrites seems to work, but now everything mocked with jest.mock seems to break for some reasons. Is there any known breaking change related to that?
I was facing the same issue and while implementing detox e2e test. fixed it by adding the following code in the e2e/config.json
under the main {}. my final config.json file looks like this:
@michaelknoch I had to install
babel-jestas per https://jestjs.io/docs/en/getting-started#using-babel. I also had to rename my.babelrctobabel.config.js(and addmodule.exports =to the top since the json file is now a regular js file). Here is my final list of babel depencies:This solution worked for me but I also had to bring
@babel/plugin-proposal-class-propertiesto mydevDependencieshttps://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-class-propertiesAfter spending a couple of hours trying to make everything work, I noticed there is some official documentation for ts-jest to run on react-native 0.57 and babel 7:
https://kulshekhar.github.io/ts-jest/user/react-native/
Solved my problem
I’m running into the same issue as @michaelknoch
This file is no longer available since 0.70
It works for me. I didn’t change my ‘.babelrc’ though. Just installed the ‘babel-core’
I had the same issue and adding this to jest config made test work
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",But then myjest.mock()statements were not working. It seems all the jest.mock() calls need to be hoisted before allimportstatements in the test.@ajcrites I tried your solution it works great locally but for some reason my CI is throwing this error.
Just echoing other solutions above… depending on your use case, use only one:
.babelrcorbabel.config.js(as per documentation)For me, I was in the process of migrating my code and had both files present by accident, so the bundler was confused. I removed
.babelrcand keptbabel.config.jsand the error went away.@makabde It worked for me too, without adding
@babel/plugin-proposal-class-propertiesI finally resolved the issue I was having by bringing in
https://www.npmjs.com/package/babel-plugin-jest-hoistto my project.My dependencies:
and my
babel.config.js:EDIT: my tests are now passing locally but failing on CI 🙇♂️
Looks like version 0.47 of
metro-react-native-babel-presetis available now. Testing now works for me with this new version includingjest.mock.