Blog Details

An Experience based comparison between Mocha & Jasmine Test Frameworks

An Experience based comparison between Mocha & Jasmine Test Frameworks

This is the 3rd article in our series of blogs on Test Automation Framework

There are many open sources available in market; most talked frameworks are Jasmine and Mocha. In past, we have developed our own framework using shell script, Java to test REST APIs and backend services. Whether you are 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? Now days having such flexible frameworks available in market, it makes test engineers life much easier and simple. I do not have much experience on other tools in market, but in recent past I have explored two frameworks Jasmine and Mocha. Just trying to put together about the frameworks as per my limited knowledge in this space.

Mocha is a JavaScript framework for Node.js which allows asynchronous testing. In other words, it provides the environment in which we can use our favorite assertion libraries to test the code. I found, Mocha is significantly more flexible, but you must 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 must pick and choose how you want Mocha to work. Maybe for this flexibility (great to build the test environment that you need) or because it’s easy to switch from Jasmine to Mocha, JavaScript users are starting to prefer it above other test frameworks.

However, 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 nice in most scenarios you might need, asynchronous code being the main problem area.

Mocha

Jasmine

Simple setup

Simple setup through jasmine-node

Headless running out of the box.

Headless running out of the box

Allows use of any assertion library that will throw exceptions on failure

Fluent syntax for built-in assertions, can be extended with some other assertion libraries

Lots of plugins, highly extensible

Supported by many CI servers and some that don’t support native plugins

It works rather nice with asynchronous testing

Descriptive syntax for BDD paradigm

Supported by some CI servers and plugins for others

AngularJS usage is widely supported

Has aliases for functions to be more BDD or TDD-oriented

Expects a specific suffix to all test files

Asynchronous testing is a breeze

Asynchronous testing is rather difficult.

 

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.

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.

Conclusion

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

    13-06-2017         1 : 1 PM

Social Links