Babel unexpected token import when running mocha tests

For Babel <6

The easiest way to solve this problem is:

  1. npm install babel-preset-es2015 --save-dev
  2. Add .babelrc to the root of the project with contents:

    {
     "presets": [ "es2015" ]
    }
    

Ensure that you are running mocha with the “–compilers js:babel-core/register” parameter.

For Babel6/7+

  1. npm install @babel/preset-env --save-dev
  2. Add .babelrc to the root of the project with contents:

    {
     "presets": [ "@babel/preset-env" ]
    }
    

Ensure that you are running mocha with the --compilers js:babel-register (Babel 6) or --compilers js:@babel/register (Babel 7) parameter

For mocha version 7 or later, use --require babel-register or --require @babel/register respectively.

Leave a Comment