Skip to content

Probably not compiling with babel: Unexpected import token #1309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
renatorib opened this issue Mar 15, 2017 · 7 comments
Closed

Probably not compiling with babel: Unexpected import token #1309

renatorib opened this issue Mar 15, 2017 · 7 comments

Comments

@renatorib
Copy link

renatorib commented Mar 15, 2017

Description

Testings are failing in Unexpected import token (probably due to non babel compiling issue)

Test Source

initials.js

const initials = (string: string) =>
  string.split(' ').map(word => word[0]).join('').slice(0, 2);

export default initials;

initials.test.js

import test from 'ava';
import initials from './initials';

test(t => t.expect(initials('Renato Ribeiro')).toBe('RR'));

Error Message & Stack Trace

Here's a print (Sorry, I have a problem with my term that I can't copy text 😢)

image

Config

package.json

{
  "...": "...",
  "ava": {
    "babel": "inherit"
  }
}

.babelrc

{
  "presets": [
    "next/babel"
  ],
  "plugins": [
    "transform-export-extensions"
  ]
}

Note: also tested with "require": ["babel-register"] in ava configuration

Command-Line Arguments

ava

Relevant Links

Repo: https://github.com/renatorib/next-jsonplaceholder
Initials test commit: renatorib/next-jph@f5602a1

Environment

$ ava --version
0.18.2

$ node --version
v7.5.0 
@novemberborn
Copy link
Member

(probably due to non babel compiling issue)

Yes.

You've disabled AVA's default transpilation of test files. It's now up to you to make sure your code runs in Node.js. You'll have to make sure to transpile the module syntax in your own Babel config.

@renatorib
Copy link
Author

renatorib commented Mar 15, 2017

@novemberborn

You've disabled AVA's default transpilation of test files.

Where?

My custom babel config (next/babel) support import/export and latest es versions.
And when I remove this config:

"ava": {
  "babel": "inherit"
}

This error persists.


Why closed issue, since this not resolved?

@novemberborn
Copy link
Member

And when I remove this config:

"ava": {
"babel": "inherit"
}
This error persists.

I actually get this error, which seems to be some Flow syntax that isn't removed:

❯ npm t

> Placeholder@ test /private/var/folders/_6/p8qxp_3n62zg9081tvb0lcc80000gn/T/tmp.zWXKadVBb1/next-jsonplaceholder
> ava

/private/var/folders/_6/p8qxp_3n62zg9081tvb0lcc80000gn/T/tmp.zWXKadVBb1/next-jsonplaceholder/utils/initials.js:3
const initials = (string: string) =>
                        ^
SyntaxError: Unexpected token :

In any case, it means AVA transpiled the ESM syntax correctly.

My custom babel config (next/babel) support import/export and latest es versions.

But does it transpile to CJS?

Why closed issue, since this not resolved?

This issue tracker is for AVA itself. Happy to do the occasional support here, and happy to keep discussing, but this is a problem with your Babel setup, not AVA.

@penge
Copy link

penge commented Jan 11, 2018

Put those to package.json:

{
  "ava":  {
    "require": ["babel-register"]
  },

  "babel": {
    "presets": ["@ava/stage-4"]
  }
}

Source: transpiling-sources@AVA

@avajs, I would suggest making init more clever so those issues are gone.
To be "No extra setup needed." :)

@mdvorscak
Copy link
Contributor

I would like to add that it is very un-intuitive that you must add

"ava":  {
    "require": ["babel-register"]
  }

In order to get ava working with ESNext source files and the documentation here is very misleading:

By default our Babel pipeline is applied to test and helper files ending in .js. If your project uses Babel then we'll automatically compile files using your project's Babel configuration.

Since you must add the snippet from above, which is given at the bottom of the doc.

@novemberborn
Copy link
Member

@mdvorscak that paragraph does attempt to specify "test and helper files", but I agree it's not entirely clear. Perhaps "files" in the second sentence should be "these files", and we should add a paragraph linking to source compilation section?

@mdvorscak
Copy link
Contributor

I agree, I created a PR to attempt to clarify this behavior.

novemberborn pushed a commit that referenced this issue Feb 12, 2018
Following discussion of #1309, this PR attempts to clarify the documentation to avoid confusion when first setting up AVA with ESNext source files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants