ava:SyntaxError:意外的令牌导入 [英] ava: SyntaxError: Unexpected token import
问题描述
因此ava附带了内置ES2015支持,对于我的实际情况来说效果很好测试文件.但是,当我这样做
So ava comes with build-in ES2015 support, which works fine for my actual test files. However, when I do
import {newUser, createUser, login} from './helpers/user';
我不能使用import
并导出到帮助文件中,然后我得到:
I can't use import
and export in the helper file, I then get:
Users/rowe/Code/fv/foxvision-api/test/api/helpers/user.js:1
(function (exports, require, module, __filename, __dirname) { import request from 'supertest';
SyntaxError: Unexpected token import
对于开箱即用的测试文件,我没有设置特定的babel配置.谁能向我解释为什么不使用babel转换帮助程序依赖项?甚至遵循 ava约定使用.
I have no specific babel configuration set up as for the test files it works out of the box. Can anyone explain to me why the helper dependencies are not transpiled with babel? Using test/**/helpers
is even following ava convention.
谢谢, 罗宾
解决方案
因此,根据 thangngoc89 的解决方案,我所做的工作是:
So based on thangngoc89's solution, what I did to make it work was:
- 添加具有内容的
.babelrc
{
"presets": [
"es2015",
"stage-2"
],
"plugins": [
"espower",
"transform-runtime"
]
}
- 已添加到
package.json
:
"ava": {
"require": ["babel-register"],
"babel": "inherit"
}
推荐答案
AVA仅转储测试文件.不测试依赖项,因此您将需要在项目中设置babel(我想是因为您一直在使用ES6才这样做的.)
AVA only transpile the test files. Not test dependencies so you will need to setup babel in your project (I suppose you did it because you're using ES6 anyway).
然后在AVA的设置中,添加以下内容:
Then in AVA's setting, add this :
"ava": {
...
"babel": "inherit"
}
这意味着使用您的项目babel设置来转换测试依赖项.请参阅AVA文档中的更多信息: https://github.com/sindresorhus/ava/blob/master/docs/recipes/babelrc.md
It means that use your project babel setting to transpile the test dependencies. See more information in AVA docs: https://github.com/sindresorhus/ava/blob/master/docs/recipes/babelrc.md
这篇关于ava:SyntaxError:意外的令牌导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!