3

I am using the least minimal setup for a react application:

  • Webpack
  • Babel
  • React + Flux
  • Mocha & Chai for testing

I want to test my app now.

I have a .babelrc with the following content:

{
  "presets": ["es2015"],
  "ignore": false
}

And my test looks like this:

import { expect, assert } from 'chai';
import AppStore from '../src/js/stores/app-store';

describe('app store', () => {
  assert.equal(3,3);
});

When I comment the second import out, it works.

When I import my AppStore, I am getting this error message:

(function (exports, require, module, __filename, __dirname) { import { dispatch, register } from '../dispatchers/app-dispatcher';
                                                              ^^^^^^

SyntaxError: Unexpected token import

So, I am apparently transpiling the test.js file, but the imports won't transpile to ES5.

What can I do, how does a minimal setup look like (without using Grunt or whatever).

EDIT: My node scripts inside the package.json look like this:

  "scripts": {
    "dev": "webpack && webpack-dev-server",
    "test": "mocha --compilers js:babel-core/register --recursive"
  },
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
ohboy21
  • 4,259
  • 8
  • 38
  • 66
  • 3
    `mocha --compilers js:babel-core/register --recursive` should be `mocha --compilers js:babel-register --recursive`. Depending on different versions ofc. – Henrik Andersson Jan 18 '16 at 11:10

1 Answers1

0

I ran into that same issue. Having tried every other solution on stackoverflow and beyond, this simple config on package.json did it for me:

  "babel": {
    "presets": [
      "es2015"
    ]
  }

All my ES6 imports worked after that. By the way, I had this same configuration inside webpack.config.js, but apparently this was the only way to make it work for mocha testing as well.

arakno
  • 428
  • 3
  • 6