Follow

Jasmine vs Mocha - JavaScript Testing Framework Comparison

Whether you’re writing javaScript for the browser or for node JS, the question always exists: what unit test library should I use to ensure my javascript code is working as expected? If you were considering one of the two popular JavaScript testing frameworks: Jasmine, or Mocha, then we’ve got some information you might be interested in.

Jasmine

Jasmine is easier to get started, it was built to be easy to set up and use in almost any scenario. It requires a runner, such as Karma or Chutzpah, in most scenarios, but some distros have one baked in. Jasmine is the most widely used test framework for those who do testing in AngularJS. So, if you use AngularJS it should be probably among the first choices. In addition, it works pretty nice in most scenarios you might need, asynchronous code being the main problem area.

The nice things:

  • - Simple setup through jasmine-node.
  • - Headless running out of the box.
  • - Fluent syntax for built-in assertions, can be extended with some other assertion libraries.
  • - Supported by many CI servers and some that don’t support native plugins.
  • - Descriptive syntax for BDD paradigm.
  • - AngularJS usage is widely supported.

The ugly things:

  • - Asynchronous testing is rather difficult.
  • - Expects a specific suffix to all test files.

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 into Mocha, so you’ll have to pick one. You can also configure Mocha for BDD (jasmine style) or TDD easily. In fact, you have to pick and choose how you want Mocha to work.

Maybe for this flexibility (great to build the test environment that you really need) or because it’s easy to switch from Jasmine to Mocha, JavaScript users are starting to prefer it above other test frameworks.

The nice things:

  • - Simple setup.
  • - Headless running out of the box.
  • - Allows use of any assertion library that will throw exceptions on failure.
  • - Lots of plugins, highly extensible.
  • - It works rather nice with asynchronous testing.
  • - Supported by some CI servers and plugins for others.
  • - Has aliases for functions to be more BDD or TDD-oriented.
  • - Asynchronous testing is a breeze.

The ugly things:

  • - It remains to be the newer on the field, so sometimes there could be a lack of support in certain areas.

Concluding

To conclude, it is important to point out that, although Jasmine does not have a test runner and Mocha on the other hand includes one and an API for setting up the test suite, both libraries are supported in the popular test runner Karma. Karma can run your tests against different browsers. So, if you were to choose a test framework setup today, you will likely use Karma for the test runner. This gives you continuous feedback while developing your code.

Hopefully this article has made it more clear as to what some of the main differences are between Jasmine and Mocha. If you have any questions, please do not hesitate to contact us. We will be happy to help you.





Brian Lamilla

About Brian Lamilla

Brian is a Web Developer and SEO / SEM / CMS specialist with more than 5 years of experience positioning Websites for local and global Customers. He is also a QA Specialist with more than 5 years of experience testing Web and Desktop applications for some of the most important Fortune 500 companies.

Nowadays Brian specializes in leading the SEO/ SEM department of TISA, continuously researching about new positioning algorithms to obtain a better performance and rating in a lower time and also works at the QA department of TISA, looking for implementing and working with the latest testing technologies and applications to be used in future projects so quality can be reached in a higher level.

Beyond his technical knowledge and passion for the technology, Brian enjoys playing video games, going to the Gym and watching and playing soccer and tennis.