mocha: SyntaxError: Unexpected token import

I spent so much time making it work, and it doesn’t.

package.json

"scripts": {
    "test": "node_modules/mocha/bin/mocha --require @babel/register"
},
"devDependencies": {
    "@babel/cli": "^7.1.0",
    "@babel/core": "^7.0.1",
    "@babel/plugin-proposal-class-properties": "^7.0.0",
    "@babel/plugin-proposal-object-rest-spread": "^7.0.0",
    "@babel/preset-env": "^7.1.0",
    "@babel/preset-react": "^7.0.0",
    "@babel/register": "^7.0.0"
    "mocha": "^5.2.0"
}

.babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/react"
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-proposal-object-rest-spread"
  ]
}

npm test <path to file> says: SyntaxError: Unexpected token import

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 17 (5 by maintainers)

Most upvoted comments

Did you try?

$ NODE_DEBUG="fs" DEBUG="*" node_modules/mocha/bin/mocha --require @babel/register

And if you use some library, you should handle its debug info. Thats my opinion.

We didn’t use the library; you did. Outsider attempted to help you but can’t reproduce this. We’re not responsible for babel support, but you might try here or here.

I use the lower version mocha to solve it. npm i mocha@^7.0.0 -g and run like this: mocha xxx.js

As a side note, it’s unnecessary to specify the path to “mocha” within “package.json”; npm will automatically add “node_modules/.bin” to your PATH variable. So write it like this instead.

“package.json”

  "scripts": {
    "test": "mocha --require @babel/register"
  },

@stujo fast workaround as i remember was to rename client/package.json before testing, but currently i don’t work on that project.

@outsideris …/node_modules/.bin/mocha --require @babel/register src/actions/helpers.test.js

it doesn’t work. SyntaxError: Unexpected token import