没有 .babelrc 的 Jest 错误 React Native 错误
Jest errors without .babelrc React Native errors with it
简而言之,我现有的 React Native 应用程序在没有 .bablerc 的情况下工作,我只能让 Jest 使用 .babelrc(因为我使用 es6 语法),一旦我添加了 .babelrc,我的 React Native 应用程序就会中断以下错误。
添加 .babelrc 后,我在我的 react-native 服务器中看到几个 [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 预设隔离为仅 运行?有没有我缺少的更简单的解决方案,可以让开玩笑和本机反应并肩工作? .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 用于构建应用程序的东西不完全相同)。这个预处理器在 jestSupport/
目录中随 RN 一起提供,你应该在你的 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 应用程序在没有 .bablerc 的情况下工作,我只能让 Jest 使用 .babelrc(因为我使用 es6 语法),一旦我添加了 .babelrc,我的 React Native 应用程序就会中断以下错误。
添加 .babelrc 后,我在我的 react-native 服务器中看到几个 [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 预设隔离为仅 运行?有没有我缺少的更简单的解决方案,可以让开玩笑和本机反应并肩工作? .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 用于构建应用程序的东西不完全相同)。这个预处理器在 jestSupport/
目录中随 RN 一起提供,你应该在你的 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 来 运行 测试。