question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Probably not compiling with babel: `Unexpected import token`

See original GitHub issue

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: https://github.com/renatorib/next-jsonplaceholder/commit/f5602a1f77163ca9a0fdac4f9bd76ccf10955fcd

Environment

$ ava --version
0.18.2

$ node --version
v7.5.0 

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:6
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
pengecommented, 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.” 😃

4reactions
novemberborncommented, Mar 16, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"unexpected token import" in Nodejs5 and babel?
For me it was a simple fix. I was caught in tunnel vision trying to adapt code from one react/babel project into another...
Read more >
SyntaxError: Unexpected token - JavaScript - MDN Web Docs
The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided.
Read more >
How I Fixed The Unexpected Token Error In Jest
I saw it on line 1, because line 1 is almost always occupied by an import statement - and there are no import...
Read more >
syntaxerror unexpected token export babel-jest - You.com
This means, that a file is not transformed through TypeScript compiler, e.g. because it is a JS file with TS syntax, or it...
Read more >
Import React in Node.js throws SyntaxError - appsloveworld.com
That's because you are transpiling only ./src/server and not ./src/admin . Then in ./src/server you have export keyword not translated by Babel and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found