5

我尝试使用AVAbabel-preset-react-native测试我的 react-native 应用程序

我的配置如下所示:

"scripts": {
  "test": "ava"
},
"ava": {
  "files": [
    "src/**/__tests__/*.js"
  ],
  "failFast": true,
  "require": [
    "react-native-mock/mock.js",
    "babel-register"
  ],
  "babel": {
    "presets": [
      "react-native"
    ]
  }
},
"devDependencies": {
  "ava": "^0.13.0",
  "babel-preset-react-native": "^1.2.4",
  "babel-register": "~6.4.3",
  "react-native-mock": "0.0.6"
}

......并像这样失败:

/Users/zoon/Projets/xxxxx/node_modules/babel-register/node_modules/babel-core/lib/transformation/file/index.js:556
      throw err;
      ^

SyntaxError: /Users/zoon/Projets/xxxxx/src/reducers/env.js: Unexpected token (12:8)
  10 |     case types.RECEIVE_CHANGE_ENV:
  11 |       return {
> 12 |         ...state,
     |         ^
  13 |         current: Environments[action.env]
  14 |       };
  15 |     default:

如果我在 .babelrc 文件中导出这个 babel 配置并在我的 AVA 配置中使用 "babel": "inherit",它会以另一种方式失败:

/Users/zoon/Projets/xxxxx/node_modules/lodash-es/lodash.js:10
export { default as add } from './add';
^^^^^^

SyntaxError: Unexpected token export

我不明白如何正确配置它。我试过摩卡,遇到同样的问题。

4

1 回答 1

3

babel-register默认忽略node_modules。您可以ignore:false在您的设置中.babelrc禁用该行为。您可以(可能应该)还指定一个更有限的正则表达式,以避免处理node_modules.

请参阅文档:https ://babeljs.io/docs/usage/require/

您的完整配置可能看起来像这样:


.babelrc

{
  "presets": ["react-native"],
  "ignore": false
}

包.json

{
  "ava": {
    "babel": "inherit"
  }
}
于 2016-03-16T19:02:52.337 回答