One of the crucial decisions in writing automated tests for web applications is choosing the best-suited testing framework. There are a number of frameworks and several unique approaches one can take to writing tests for your application. Three such frameworks that we consider at QualityWorks are QUnit, Jasmine and Mocha. Here’s a quick rundown and evaluation of each framework to help you in choosing the ideal one to match your needs:
QUnit
QUnit is a powerful JavaScript unit testing framework that helps you to debug code. It’s written by members of the jQuery team and is the official test suite for jQuery. But QUnit is general enough to test any regular JavaScript code, and as such we can use it to test our NodeJS apps.
Pros
- Lots of support across the board, from Q&A to CI server support
Cons
- Lacks fluent syntax
- Configuration is a headache, and must constantly be maintained
- Makes including 3rd party libraries (like assertion libraries) relatively difficult
- Asynchronous testing can be a bit of a headache
- No baked-in headless run support
QUnit is about 10 years old now and in the Javascript world that makes you a grandpa. It does have the benefit of having solid documentation and support but a lot of the tasks that are harder to accomplish such as Asynchronous testing has been simplified in some of the new libraries.
Jasmine
Jasmine is a behaviour-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests. It’s 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 (like the jasmine-node npm) have one baked in. It’s pretty nice for most scenarios you could want, asynchronous code being the main problem area.
Here’s an example of what a test that is written with Jasmine as the test framework looks like
describe(“A suite is just a function”, function() {
var a;
it(“and so is a spec”, function() {
a = true;
expect(a).toBe(true);
});
});
Pros
- Simple setup for node through jasmine-node
- Headless running out of the box
- Nice fluent syntax for assertions built-in
- Supported by many CI servers
- Descriptive syntax for BDD paradigm(which is a big plus for us)
Cons
- Asynchronous testing can be a bit of a headache
- Expects a specific suffix to all test files (*spec.js by default)
Jasmine is a couple years younger than QUnit and does have a very clean syntax and is pretty easy to use and we have used it for a few projects here at QualityWorks. Another plus for Jasmine is that it does play pretty well with other assertion libraries which is an important feature to the team here.
Mocha
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting while mapping uncaught exceptions to the correct test cases. Having been built specifically for testing NodeJS modules, Mocha is the baby of the bunch with its first major build released in 2012. Its API is rather similar to that of Jasmine’s, with a bit of syntactic sugar added to make it more conducive to a wider range of scenarios, such as BDD. It has its own test runner baked in, so that’s a concern you should never have to worry about. It also, unlike Jasmine, has really nice support for testing asynchronous methods, using the done() function: if your test uses it, the test doesn’t pass until done() is called, if it doesn’t use it, the test will pass when it reaches the end of the test method.
Pros
- Clear, Simple API
- Headless running out of the box
- Allows use of any assertion library that will throw exceptions on failure, such as Chai
- Supported by some CI servers
- Has aliases for functions to be more BDD-oriented or TDD-oriented
- Highly extensible
- Asynchronous testing is very simple
Cons
- Tests cannot run in random order.
- Allows use of any assertion library(Both pro and con)
- No auto mocking or snapshot testing
We added “Allows use of any assertion library” as both a pro and con because while some testing frameworks are completely out of the box, Mocha requires developers to select and set up assertion libraries and mocking utilities. For someone who is just starting to learn how to build tests, this can be scary as they will also have to choose which libraries to use and learn them too.
In our case at QualityWorks, we’re quite happy with that feature and consider it a big Pro. We’ve discovered that Mocha has the easiest configuration, most flexibility, and best usability. Out-of-the-box, it does everything you need it to and does so elegantly. Taking all of this into consideration, we recommend Mocha as the Test Framework of Choice when testing NodeJS apps.