Using mocha with ES6+ spec files

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:

  1. npm install mocha babel-register @babel/preset-env --save-dev
  2. make your npm test script sth like this: mocha --require babel-register
  3. now write all your unit tests in ES6+
  4. invoke npm test and benefit

Please note that you could further edit your .babelrc file to match your desired environment / requirement. Learn more here.

Over and out,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s