React Native with Typescript and Jest is broken after 0.57 Update: Couldn't find preset “module:metro-react-native-babel-preset” relative to directory

こ雲淡風輕ζ 提交于 2019-12-02 20:45:16

I found the answer here in this issue: https://github.com/facebook/metro/issues/242#issuecomment-421139247

Basically, add this to your Jest section in package.json:

"transform": { "^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js" }

had a similar issue when upgrading to 0.57, my package.json still contained an entry for babel-preset-react-native (which is now deprecated in favor of metro-react-native-babel-preset). All I had to do was

yarn remove babel-preset-react-native

and then

yarn add metro-react-native-babel-preset --dev

Lastly, make sure you change your .babelrc from

{
  "presets": ["react-native"]
}

to

{
  "presets": ["module:metro-react-native-babel-preset"]
}

More info can be found here

If I use "presets": ["react-native"] then production/development is broken but tests are working.

If I use "presets": ["module:metro-react-native-babel-preset"] then tests are broken but production/development is working.

Not sure why it's like that but a .babelrc solution for me like below works both for development/production and tests. Just add env parameter to your .babelrc file.

"env": {
    "test": {
        "presets": ["react-native"]
    },
    "production": {
        "presets": ["module:metro-react-native-babel-preset"]
    },
    "development": {
        "presets": ["module:metro-react-native-babel-preset"]
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!