Okay, so the problem is obvious: we want to use
mocha to run spec .js files written in ES6+ ( very likely with
async await being utilised ‘cuz everybody loves ’em 😉 ).
I’ll go ahead and state it upfront that: there is nothing special or challenging about this. I’m blogging this only for the sheer reason that I’m so forgetful these days that I’d love something I could copy and paste into my existing workflow.
This ‘copy & paste’ method is valid & up-to-date as of Feb 2018, so if you’re from the future, please give it a double check whether it’s still worth following.
Without further ado:
npm install mocha babel-register @babel/preset-env --save-dev
- make your
npm testscript sth like this:
mocha --require babel-register
- now write all your unit tests in ES6+
npm testand benefit
Please note that you could further edit your
.babelrc file to match your desired environment / requirement. Learn more here.
Over and out,