I have tried to run AVA against against babel preset 'es2017' but it fails with the following error
import test from 'ava';
^^^^^^
SyntaxError: Unexpected token import
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:528:28)
at extensions.(anonymous function) (/Users/carlos/dev/sedaAds/ads-loader/node_modules/require-precompiled/index.js:13:11)
at Object.require.extensions.(anonymous function) [as .js] (/Users/carlos/dev/sedaAds/ads-loader/node_modules/ava/lib/test-worker.js:92:3)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/carlos/dev/sedaAds/ads-loader/node_modules/ava/lib/test-worker.js:96:1)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
1 exception
✖ test/adsLoader.spec.js exited with a non-zero exit code: 1
The test is the simplest test ever
'use strict';
import test from 'ava';
test('test', t => t.pass());
With this configuration in the package.json the test fails
...
"devDependencies": {
"ava": "^0.16.0",
"babel-cli": "^6.16.0",
"babel-core": "^6.16.0",
"babel-eslint": "^7.0.0",
"babel-loader": "^6.2.5",
"babel-preset-es2017": "^6.16.0",
"babel-preset-es2015": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"babel-register": "^6.16.0",
"eslint": "^3.6.1",
"webpack": "^2.1.0-beta.21"
},
"babel": {
"presets": [
"es2017",
"stage-0"
]
},
"ava": {
"require": "babel-register",
"babel": "inherit"
}
It only works if I use "es2015"
"babel": {
"presets": [
"es2015",
"stage-0"
]
},
"ava": {
"require": "babel-register",
"babel": "inherit"
}
Any idea on why is this happening?
Is test set as a default export of ava? If not you may need to use
import { test } from 'ava';
I could be completely wrong though.
@Sntax import test from 'ava'; should work fine.
@carpasse When you set the Babel plugins to use, you are overriding the ones AVA uses by default.
babel-preset-es2017 only contains the additions to the language that were made in the ES2017 spec (or will be in this case), but the modules syntax is something that was added in ES2015 and is thus present in babel-preset-es2015.
If you use a recent version of Node, you may not need to use babel-preset-es2015, but if you wish to use the import syntax, you should also add the babel-plugin-transform-es2015-modules-commonjs plugin. Either that, or use babel-preset-es2015, babel-preset-es2016 and babel-preset-es2017.
@jfmengels thanks for the clarification after thinking it through we will stay with babel-preset-es2015
Most helpful comment
@Sntax
import test from 'ava';should work fine.@carpasse When you set the Babel plugins to use, you are overriding the ones AVA uses by default.
babel-preset-es2017only contains the additions to the language that were made in the ES2017 spec (or will be in this case), but the modules syntax is something that was added in ES2015 and is thus present inbabel-preset-es2015.If you use a recent version of Node, you may not need to use
babel-preset-es2015, but if you wish to use the import syntax, you should also add thebabel-plugin-transform-es2015-modules-commonjsplugin. Either that, or usebabel-preset-es2015,babel-preset-es2016andbabel-preset-es2017.