Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test harmony / ES6 / ECMAScript 6 JavaScript?

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.

like image 397
superjos Avatar asked Oct 10 '14 17:10

superjos


2 Answers

I've been using

npm install mocha-traceur

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

and it's been working like a charm !

like image 185
Joel Lord Avatar answered Sep 24 '22 19:09

Joel Lord


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.

like image 37
Andreas Rossberg Avatar answered Sep 22 '22 19:09

Andreas Rossberg