Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 import for 'ava' test not working

I followed the docs to create my first test using ava but it doesn't seem to run properly. I get the error below. I tried adding import 'babel-register'; at the top of the file, and it works, but only if I run one specific test file. e.g. ava ./test/helpers/test_helper.js. Running ava on its own though... results in the import error below. Does anyone else know how to fix this? The getting started guide uses ES6 import and I have no idea why mine doesn't just work.

(function (exports, require, module, __filename, __dirname) { import test from 'ava'; ^^^^^^ SyntaxError: Unexpected token import

test.js

import test from 'ava';

test(t => {
  t.deepEqual([1, 2], [1, 2]);
});
like image 786
Clement Avatar asked Jan 17 '17 08:01

Clement


People also ask

Does ES6 Import Export?

With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.

Can ES6 module have side effects?

Examples of side effects: A polyfill that enables ES6 features in the browsers that don't support them, like babel polyfill is a side effect. Many jQuery plugins attach themselves to the global jQuery object. Analytics modules that run in the background, monitor user interaction, and send the data to a server.


Video Answer


3 Answers

there is a far easier way to work with ES module for AVA

$ npm install esm --save-dev 

Then in your package.json add


{
    "ava": {
        "require": [
            "esm"
        ]
    }
}

Babel never works correctly, I spend more time on debugging the tool then my code with all this pile of CS everyday!

like image 167
Joel Chu Avatar answered Oct 19 '22 13:10

Joel Chu


Add to your package.json

"ava": {
  "files": [
    "test/**/*.js"
  ],
  "require": [
    "babel-register"
  ],
  "babel": "inherit"
},

Your .babelrc

{
  "presets": ["es2015"]
}

And then your imports should work.

like image 34
user2832344 Avatar answered Oct 19 '22 12:10

user2832344


add this to your package.json

 "ava": {
    "babel": true
  }

e.g.

https://github.com/e2e-boilerplate/selenium-webdriver-es-modules-babel-ava/blob/master/package.json

https://github.com/e2e-boilerplate/puppeteer-es-modules-babel-ava/blob/master/package.json

like image 25
user5249270 Avatar answered Oct 19 '22 11:10

user5249270