JavaScript Unit Tests: Jasmine vs Mocha


Someone recently asked me whether I prefer Jasmine or Mocha for unit testing JavaScript. My answer is:

Jasmine and Mocha are both great. I use both, depending on the project and team. There’s a great community around both, and you’ll find everything you need for either.

Jasmine

Jasmine is easier to get started – it’s all-in-one package will generally get you and a team up and testing much faster, and you’ll be in good hands with it.

Mocha

Mocha is significantly more flexible, but you have to piece it together yourself.

There is no spy framework built in to Mocha, so most people use sinon.js. There’s no assertion framework built in to Mocha either, so you’ll have to pick one. I like Chai for that, but there are many, many others available. You can also configure Mocha for BDD (jasmine style) or TDD (qunit style) easily. But you have to pick and choose how you want Mocha to work.

This flexibility is great because you can build the test environment that you really want. But it means you have more work to do, more individual pieces to maintain / keep up to date, etc.

Abstraction: The Rule Of Three