8

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]);
});
Clement
  • 4,491
  • 4
  • 39
  • 69

5 Answers5

27

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!

Joel Chu
  • 828
  • 1
  • 9
  • 25
5

Add to your package.json

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

Your .babelrc

{
  "presets": ["es2015"]
}

And then your imports should work.

Tim
  • 360
  • 6
  • 9
user2832344
  • 264
  • 2
  • 7
0

For me it was enough to just add

"type": "module",

to my package.json

in order to make

import test from 'ava';

test('foo', t => {
    t.pass();
});

run correctly.

Jannis Hell
  • 736
  • 8
  • 17
-2

After you have yarm/npm installed it, did you run ava --init?

In package.json, what does the command say? If you run (if you use npm) npm run test, it should execute the command in your package.json.

If you then have any .js (ES6) in your test directory, it should execute it (example is also on their github page https://github.com/avajs/ava).

You don't need to add all of that which is mentioned in the above comment. These commands should get you a working run:

mkdir avatest
cd avatest
npm init
npm install --global ava (you probably did this already)
npm install --save-dev ava
ava --init
touch test/test.js
atom test/test.js (pasted your script)
npm run test
> 1 passed
Angelo Michel
  • 265
  • 1
  • 5
  • Downvoted because this answer addresses setting up AVA but the question asked is about the error that occurs when a file you're importing into the test file uses "import" or "export" or whatever feature that needs to be transpiled. AVA mentions that it doesn't transpile code that in imported into a test but does show how you might get it working: https://github.com/avajs/ava#transpiling-imported-modules – Sgnl Sep 28 '18 at 01:13
  • Alternatively, it could be downvoted because it does not work. First, the list of instructions doesn't mention that you need to set "test": "ava" inside the "script" key in your package.json. After that, I get the error message "TypeError: Tests must have a title". This is likely due to me using a newer version of ava: 2.4.0. A horrible change if you ask me since the output gives you the line number, so there's no need to name each test. – John Deighan Dec 02 '19 at 20:57