JavaScript Testing: A Breakdown

Experienced developers agree that testing your code is important. Testing is a big subject worthy of thousands of blog articles, screencasts, conference talks, and consulting businesses. But let’s start small. One of the key steps in learning to create automated tests is creating unit tests. Unit tests are intended to test the smallest chunk of code we can (a unit).  For unit testing in Javascript, the two big testing frameworks are Jasmine  and Mocha .

The differences between the two largely boil down to set up and configurability. It’s worth noting that Jasmine is usually used with a browser and Mocha was created for testing Node (server code). However, both can be configured to test either front-end or server code.

Jasmine is a more complete testing framework out of the box. It can handle spies and mocking (more on that later) without any extra plugins. Mocha is typically paired with SinonJS for mock support. It’s also paired with Chai as an assertion library. So the two major testing stacks you see (most often) are:

  • Jasmine

  • Mocha + SinonJS + Chai

There’s not a ‘right’ answer when choosing a test framework. I like Jasmine personally, but I that’s just me. Use what you want, I’m just glad you want to write tests!


To just jump in and try it out there’s try Jasmine. I’m not currently aware of a similar site for Mocha, other than the ever popular cyber dojo, which can be used to explore unit testing in a variety of languages.

Now that you have your tools, Google is going to be your friend for finding tutorials on how to use these shiny new widgets. Whether you prefer articles, screencasts, or screencasts, all of which are available in abundance. Just for fun I did a quick search and found this tutorial for Jasmine and this tutorial for Mocha. Of note: this Mocha tutorial does not include Sinon. For beginning unit tests, I wouldn’t worry about getting into mocks and stubs just yet. It’s okay to skip Sinon for now.