简而言之,我现有的 React Native 应用程序无需 .bablerc 即可工作,我只能让 Jest 使用 .babelrc (因为我使用 es6 语法),一旦我添加 .babelrc,我的 React Native 应用程序就会中断,并显示以下内容错误。
添加 .babelrc 后,我在 react 原生服务器中看到几个 [BABEL] 注释,如果没有 .babelrc,这些注释就不存在。这是一个示例(我在 node_modules 之前删除了路径),尽管我不认为这是一个问题
[BABEL] Note: The code generator has deoptimised the styling of "/node_modules/react-native-htmlview/vendor/htmlparser2.js" as it exceeds the max of "100KB".
有没有办法将“react-native”.babelrc 预设隔离为仅与 jest 一起运行?我是否缺少一个更简单的解决方案来让 Jest 和 native react 并肩工作? .babelrc 中是否有其他预设,我需要正确地编译 React Native?
我使用的是 React Native 0.29.0,这是我的 .babelrc 和 package.json
.babelrc
{
"presets": ["react-native"]
}
package.json
{
"name": "MyApp",
"version": "0.0.1",
"private": true,
"scripts": {
"test": "jest",
"start": "node node_modules/react-native/local-cli/cli.js start",
},
"dependencies": {
"base-64": "^0.1.0",
"keymirror": "^0.1.1",
"lodash": "^4.13.1",
"moment": "^2.13.0",
"ramda": "^0.21.0",
"react": "^15.1.0",
"react-native": "^0.29.0",
"react-native-animatable": "^0.6.1",
"react-native-button": "^1.6.0",
"react-native-collapsible": "^0.7.0",
"react-native-cookies": "git://github.com/sdg9/react-native-cookies",
"react-native-drawer": "^2.2.3",
"react-native-htmlview": "^0.5.0",
"react-native-material-design": "^0.3.6",
"react-native-material-kit": "^0.3.2",
"react-native-router-flux": "^3.30.0",
"react-native-scrollable-tab-view": "^0.5.2",
"react-native-shared-preferences": "0.0.5",
"react-native-simple-store": "^1.0.1",
"react-native-spinkit": "^0.1.2",
"react-native-swiper": "^1.4.5",
"react-native-vector-icons": "^2.0.3",
"react-redux": "^4.4.5",
"react-thunk": "^1.0.0",
"redux": "^3.5.2",
"redux-actions": "^0.10.0",
"redux-logger": "^2.6.1",
"redux-thunk": "^2.1.0",
"tcomb-form-native": "^0.5.1",
"underscore": "^1.8.3"
},
"devDependencies": {
"babel-eslint": "^6.1.2",
"babel-jest": "^13.2.2",
"babel-polyfill": "^6.9.1",
"eslint": "^3.0.1",
"eslint-plugin-react": "^5.2.2",
"eslint-plugin-react-native": "^1.1.0",
"flow-bin": "^0.28.0",
"jest-cli": "^13.2.3",
"regenerator": "^0.8.46",
"regenerator-runtime": "^0.9.5",
"remote-redux-devtools": "^0.3.3"
},
"jest": {
"haste": {
"defaultPlatform": "ios",
"platforms": [
"ios",
"android"
],
"providesModuleNodeModules": [
"react-native"
]
}
}
}
最佳答案
对于 RN,Jest 通过稍微调整的 Jest 专用预处理器运行您的代码(即与 RN 用于构建应用程序的东西不完全相同)。此预处理器随 RN 一起提供在 jestSupport/
目录中,您应该在 Jest 配置中指定它:
...
"scriptPreprocessor": "node_modules/react-native/jestSupport/preprocessor.js",
"setupEnvScriptFile": "node_modules/react-native/jestSupport/env.js",
"haste": {
....
我刚刚在 RN 0.29 和 Jest CLI 13.0.0 上进行了测试,我的测试可以使用我的 package.json 中的此配置以及与您在问题中输入的内容相匹配的 .babelrc 进行编译。
另外,顺便说一句,如果您指定此预处理器,实际上不需要 .babelrc 来运行测试。
关于react-native - 没有 .babelrc 的 Jest 错误 使用它会出现 React Native 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38375228/