Mocha: Mocha compiler not working?

Created on 27 Nov 2015  路  3Comments  路  Source: mochajs/mocha

According to all the blog posts I've read, the following setup should work:

package.json (I am showing only relevant bits here):

{
  "main": "./lib/main.js",
  "scripts": {
    "test": "NODE_ENV=test _mocha",
  },
  "devDependencies": {
    "babel": "^6.1.18",
    "babel-core": "^6.2.1",
    "istanbul": "^0.4.1",
    "mocha": "^2.3.4"
  }
}

test/unit/setup.test.js:

import API from '../../';
console.log('API', API);

test/mocha.opts:

--recursive
--debug
--reporter spec
--bail
--ui bdd
--compilers js:babel-core/register
test/**/*.test.js

However, it doesn't and upon running npm test it throws this error:

import API from '../../';
^^^^^^

SyntaxError: Unexpected reserved word

It seems like the code isn't getting transpiled despite being instructed to by the compiler.

Most helpful comment

I'm still seeing this, even after adding a .babelrc file.

My package.json

{
  "scripts": {
    "test": "mocha --compilers js:babel-core/register test/**/*spec.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel": "^6.3.26",
    "babel-core": "^6.4.5",
    "babel-preset-es2015": "^6.3.13",
    "babel-register": "^6.4.3",
    "chai": "^3.4.1",
    "chai-immutable": "^1.5.3",
    "mocha": "^2.3.4"
  },
  "dependencies": {
    "immutable": "^3.7.6"
  }
}

and my .babelrc file

{ 
  presets: ["es2015"],
  plugins: ["transform-es2015-modules-commonjs"]
}

All 3 comments

Going to deploy a readme for this and how I resolved it...

It will be available at: https://github.com/niftylettuce/node-mocha-babel-test-boilerplate

TLDR; Basically you need to define a .babelrc file with { presets: ["es2015"] } inside.

I'm still seeing this, even after adding a .babelrc file.

My package.json

{
  "scripts": {
    "test": "mocha --compilers js:babel-core/register test/**/*spec.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel": "^6.3.26",
    "babel-core": "^6.4.5",
    "babel-preset-es2015": "^6.3.13",
    "babel-register": "^6.4.3",
    "chai": "^3.4.1",
    "chai-immutable": "^1.5.3",
    "mocha": "^2.3.4"
  },
  "dependencies": {
    "immutable": "^3.7.6"
  }
}

and my .babelrc file

{ 
  presets: ["es2015"],
  plugins: ["transform-es2015-modules-commonjs"]
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

wzup picture wzup  路  3Comments

jamietre picture jamietre  路  3Comments

delta62 picture delta62  路  3Comments

enigmatic00 picture enigmatic00  路  3Comments

Swivelgames picture Swivelgames  路  3Comments