没有.babelrc的Jest错误React Native错误

时间:2016-07-14 13:12:26

标签: react-native jestjs

简而言之,我现有的React Native应用程序在没有.bablerc的情况下工作,我只能让Jest使用.babelrc(因为我使用es6语法),并且一旦我添加.babelrc,我的反应本机应用程序就会破坏以下内容错误。

React Native Error Image

添加.babelrc后,我看到我的反应原生服务器中的几个[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运行?我是否有一个更容易解决的问题,让我们既能开玩笑又能做出原生的反应?在.babelrc中是否有额外的预设我需要反应本机才能正确转换?

我对原生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"
      ]
    }
  }
}

1 个答案:

答案 0 :(得分:3)

对于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来运行测试。