6

我试图在运行 AVA 测试时转换源文件(及其在 node_modules 中的依赖项)。我已将 AVA 配置为需要babel-register并继承我的.babelrc文件,其中包含以下内容package.json

"ava": {
    "require": "babel-register",
    "babel": "inherit"
  }

这在.babelrc

{
  "presets": [ "es2015" ],
  "ignore": false
}

我有一个测试规范,它导入一个源文件,并且该源文件从 node_modules 导入一个 ES2015 依赖项

但是,运行时ava我看到:

/Users/me/code/esri-rollup-example/node_modules/capitalize-word/index.js:2
export default input => input.replace(regexp, match => match.charAt(0).toUpperCase() + match.substr(1));
^^^^^^

SyntaxError: Unexpected token export

这告诉我源文件 ( src/app/utils.js) 确实转换了,但它在 node_modules ( capitalize-string/index) 中的依赖项没有。

当我使用 babel CLI 时,源模块和依赖项都可以很好地转换,所以看起来.babelrc's"ignore": false设置并没有传递给babel-register. 我可以从 babel 文档中看到,您可以将忽略选项显式传递给babel-register,但我看不出如何从 AVA 配置中做到这一点。我什至尝试在导入源文件的行之前将以下内容添加到我的测试文件中,但我仍然看到相同的错误:

require("babel-register")({
  ignore: false
});

我想我可以在测试之前添加一个 transpile 步骤,但我想确保我不只是首先缺少一些 AVA 或 babel 配置。

4

1 回答 1

4

这与 babel 本身的问题有关 – https://phabricator.babeljs.io/T6726

但是您可以将babel-registerrequire 放在单独的文件中(让我们调用它.setup.js):

require('babel-register')({
    ignore: /node_modules\/(?!capitalize\-word)/i
});

const noop = function () {};

require.extensions['.css'] = noop; // If you want to ignore some CSS imports

然后更改"require": "babel-register""require": "./.setup.js"

于 2016-07-28T18:24:59.483 回答