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.

Unexpected token import when importing re-exported named exports

See original GitHub issue

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

SyntaxError: Unexpected token import

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

The issue is with importing re-exported named exports. This repo demonstrates the issue:

  • git clone https://github.com/bali182/babel-jest-export-everything-bug
  • npm install
  • npm test

To summarize: package.json

{
  "name": "babel-jest-export-everything-bug",
  "scripts": {
    "test": "jest --config .jestrc.json"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-jest": "^21.2.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "jest": "^21.2.1"
  }
}

.babelrc

{
  "presets": [
    [ "es2015", { "modules": "commonjs" } ],
    "stage-0",
    "react"
  ],
  "plugins": [
    "transform-decorators-legacy",
    "transform-runtime"
  ]
}

.jestrc.json

{
  "transform": {
    "^.+\\.jsx?$": "babel-jest"
  }
}

namedExports.js

export const x = 1
export const y = 2
export const z = 3

reExports.js

export * from './namedExports'
export default { hello: 'world' }

reExports.test.js

import Foo, { x, y, z } from './reExports'

describe('testing re-exports', () => {
  it('will never get to this method', () => {
    expect(x).toBe(1)
  })
})

Which then fails with

SyntaxError: Unexpected token import

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:305:17)
  at Object.<anonymous> (src/reExports.test.js:1:120)
      at Generator.next (<anonymous>)

What is the expected behavior?

The transpilation to succeed and the test not to fail

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system. jest: 21.2.1 (couldn’t install latest because of #5156) node: v8.9.1 npm: 5.5.1

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
gusumuzhecommented, Mar 23, 2018
1reaction
bali182commented, Feb 12, 2018

@rickhanlonii Thanks for taking the time to look at this!

I fail to see though how this is a configuration issue. I’m trying to test JS with valid syntax, which transpiles with babel/webpack, but fails to do so (with the same babel config) when using Jest and the tooling around Jest.

As far as I can see I’m using everything Jest related correctly, and tests work properly except the case when importing re-exported named exports, as the title suggests.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error importing objects: SyntaxError: Unexpected token
You need to use require to import the module and exports to export them. ES modules been implemented in node 8.5.0 and later...
Read more >
How to fix SyntaxError: Unexpected token 'export' in JavaScript?
mjs file, I have an import statement which is importing a printMyName function from the printMyName.mjs file. Which simply logs a message with ......
Read more >
typescript syntaxerror: unexpected token 'export' - You.com
The project builds and runs fine if I put the all ts files in wwwroot, remove the tsconfig and remove all imports/exports so...
Read more >
eslint-plugin-import - npm
Ensure imports point to a file/module that can be resolved. ... name ( export ); Report use of exported name as identifier of...
Read more >
JavaScript modules - MDN Web Docs
Importing features into your script. Once you've exported some features out of your module, you need to import them into your script to...
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