ES6 import for 'ava' test not working
Asked Answered
R

5

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]);
});
Reed answered 17/1, 2017 at 8:16 Comment(0)
A
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.

Almonte answered 17/1, 2017 at 8:26 Comment(2)
awesome! that fixed the error, but I get another problem. I'll post it above, would be great to hear your thoughts :)Reed
Is this still relevant for AVA 1.x?Red
H
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!

Husband answered 23/4, 2019 at 2:51 Comment(4)
I found this to be the easiest and cleaner solution.Baffle
Holy smokes. I cannot believe this just worked. I've been wading through a babel upgrade on an old site that used AVA with so much pain and frustration. Im in disbelief that esm just nailed it on the first try.Zoril
No longer supported.Mortie
I saw the no longer supported comment, but it all worked for me just now -- what am I missing?Cucumber
A
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.

Almonte answered 17/1, 2017 at 8:26 Comment(2)
awesome! that fixed the error, but I get another problem. I'll post it above, would be great to hear your thoughts :)Reed
Is this still relevant for AVA 1.x?Red
D
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.

Dinh answered 28/12, 2021 at 15:57 Comment(0)
P
-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
Phonology answered 17/1, 2017 at 8:19 Comment(2)
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: github.com/avajs/ava#transpiling-imported-modulesPasquinade
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.Waddington

© 2022 - 2024 — McMap. All rights reserved.