5

As per the title, I'm trying to test some AMD modules written in ES6 JS, running from nodejs.

I tried first with Intern: even after enabling --harmony in nodejs, I ended up facing Intern's dependency chain, where I was not able to enable Harmony in e.g. Istanbul, esprima and others (I opened an issue for that).

I then moved onto mocha, and here I'm stuck ... strangely. I enabled Harmony both for nodejs and mocha itself, here's the package.json test script:

"test": "node --harmony node_modules\\mocha\\bin\\mocha tests --harmony --recursive"

that I run from command prompt as npm test my_test_folder. Still, some ES6 constructs (like const) pass ok, but then it chokes on destructuring assignment. Here are the first output lines:

const { log, dir } = require('../consoleLogger.js');
      ^
SyntaxError: Unexpected token {
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    [...continues...]

I've also checked this SO thread and heard about transpilers, but I don't really know if they could work and I'm now trying to make transpilers work in this case.

Any idea on how to solve this, without resorting to change all ES6 bits spread in code? TA.

Community
  • 1
  • 1
superjos
  • 12,189
  • 6
  • 89
  • 134
  • Are you able to use any of harmony's features when not using Mocha? If it does not work when you are not using Mocha, it won't work with Mocha. – Louis Oct 10 '14 at 22:58
  • Good point, although the code is part of a Thunderbird plugin, where harmony features are working. Here I'm trying to run tests from (node) command line, so issues come out now. – superjos Oct 13 '14 at 00:15
  • Since nobody mentioned it, check out http://kangax.github.io/compat-table/es6/ – Jared Smith Oct 13 '14 at 00:22

2 Answers2

4

I've been using

npm install mocha-traceur

mocha ./tests/* --compilers js:mocha-traceur 

and it's been working like a charm !

superjos
  • 12,189
  • 6
  • 89
  • 134
Joel Lord
  • 2,175
  • 1
  • 18
  • 25
1

V8 does not implement destructuring yet, so it won't be available in node for a while. Block scoping (including const) is mostly implemented, but be aware that a pre-ES6 version of const was always available, so you might want to double check what you are actually observing -- you could try 'let' declarations for less ambiguity.

Andreas Rossberg
  • 34,518
  • 3
  • 61
  • 72
  • Thanks for feedback. At the very beginning I just tried running mocha (so with *plain* node) and I got error on `const`. Then I enabled `--harmony` when invoking node, and I moved forward to the next issue. Anyway, I'm trying to go for transpiling to ES5 before running mocha. – superjos Oct 13 '14 at 00:13