Ava: Fresh AVA 7.0.0-rc.1 install displays "SyntaxError: Unexpected token import" on any ES6 import source

Created on 14 Aug 2018  Â·  6Comments  Â·  Source: avajs/ava

Description

yarn init
yarn add ava

source files:

obj.js:

export default value => 1

main.js:

import value from './obj.js'
export default value

Test Source

import test from 'ava'
import obj from './main.js'
test('This will fail in AVA', t => {
    t.is(1+1, 2)
})

Error Message & Stack Trace

  1 uncaught exception

  Uncaught exception in main.js


  c:\ava\main.js:1
import value from './obj.js'
  ^^^^^^

  SyntaxError: Unexpected token import

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test:watch: `ava -- --watch --verbose`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test:watch script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\failure\AppData\Roaming\npm-cache\_logs\2018-08-14T17_06_25_188Z-debug.log

Config

{
  "name": "test",
  "version": "1.0.0",
  "scripts": {
    "test": "ava --verbose",
    "test:watch": "ava -- --watch --verbose"
  },
  "dependencies": {
  },
  "devDependencies": {
    "ava": "1.0.0-beta.7"
  }
}

Command-Line Arguments

npm run test:watch

Environment

Windows 10, Node 8.11.3 LTS, Ava 1.0.0-beta.7, npm 5.6.0

Most helpful comment

Sorry you're having problems with this @onexdata. The issue is that AVA only compiles your test files. But since you're using ES module syntax, you need to compile source files too.

Going by what you've posted here I don't think you have a Babel compilation step? That would explain why the Babel recipe didn't help — you may not have configured Babel for @babel/register to work.

You may be interested in using the esm package as is described here: https://github.com/avajs/ava/blob/v1.0.0-beta.7/docs/recipes/es-modules.md

(I'm closing this issue for housekeeping purposes, but let's keep the conversation going.)

All 6 comments

I've tried everything in this to no avail. I've googled for 2 hours now to no avail.

Tried...

this
this
this
this

...and many others. Nothing seems to do the trick. Would really like to try out Ava... seems incredibly complex to just use ES6 basic syntax...

Sorry you're having problems with this @onexdata. The issue is that AVA only compiles your test files. But since you're using ES module syntax, you need to compile source files too.

Going by what you've posted here I don't think you have a Babel compilation step? That would explain why the Babel recipe didn't help — you may not have configured Babel for @babel/register to work.

You may be interested in using the esm package as is described here: https://github.com/avajs/ava/blob/v1.0.0-beta.7/docs/recipes/es-modules.md

(I'm closing this issue for housekeeping purposes, but let's keep the conversation going.)

Well I'm trying to use AVA for node only, and Node 8.x supports ES6 / import out of the box, so there is no compilation step or need for Babel.

I'm a little puzzled what is going on since the test itself uses the import statement, yet the code the test is testing also uses it, but fails. Is there some kind of directive that tells AVA not to use ES6 that I can bypass?

This is only for server/Node/back end code and has nothing to do with the front-end and never will.

The link you gave has a link to disable AVA's use of Babel all together and here:

https://github.com/avajs/ava/blob/v1.0.0-beta.7/docs/recipes/babel.md#preserve-es-module-syntax

It suggests putting this in package.json:
"ava": { "babel": false, "compileEnhancements": false },
But when I do that I get this:
``` × Unexpected Babel configuration for AVA. See https://github.com/avajs/ava#es2017-support for allowed values. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! [email protected] test:ava --verbose`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\nsteele\AppData\Roaming\npm-cache_logs\2018-08-14T21_58_24_743Z-debug.log
````

The URL the error suggests (https://github.com/avajs/ava#es2017-support) doesn't exist (the anchor doesn't exist so it just goes to the main page)

Oh my GOSH Node doesn't even support import yet 😧 ...

Thanks.

smacks forehead

Yes it's all a little bit confusing. I wonder if we should default to CJS instead, see #1908. Would love to know if that would've helped you in this case.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nickjanssen picture nickjanssen  Â·  4Comments

clitetailor picture clitetailor  Â·  3Comments

ivogabe picture ivogabe  Â·  5Comments

sindresorhus picture sindresorhus  Â·  5Comments

niftylettuce picture niftylettuce  Â·  4Comments