Testing with NodeJS requires some test framework to work with inside NodeJS.

Our list would be something like this:

  • Mocha
  • Jasmine
  • QUnit
  • Express (no longer supported)
  • jsUnit
  • some other tools

Lets focus on the first three now in this section, they are the most widely used, with some pros and cons.

Starting wit the oldest, the QUnit.

QUnit (2008) is as one of the oldest complete frameworks. Has good user base of experience, comes in hand with JQuery usage, has good support.
Good:
– As the oldest it has a nice user experience base, has lot of support and with lots of examples on the net.
– Lot of Q&A provided on internet sites and blogs.
Not Good:
– Configuration is difficult.
– Including third party libraries is hard, requires lot of maintaining.
– Asynchronous testing is rather difficult.

Let see some interesting about Jasmine.
Jasmine (2010) is the most widely used test framework for those who do testing in AngularJS, requires a runner usually Karma, the javaScript command line tool, which is another NodeJS application that should be installed through npm. There are other runners as Chutzpah for example as well. So if You use AngularJS this should be probably among first choices. More about this framework in some other topic.
Good:
– Simple step through jasmine-node.
– Fluent syntax for built-in assertions, can be extended with some other assertion libraries.
– Consists descriptive syntax for BDD orientation.
– AngularJS usage is widely supported.
Not Good:
– Testing with plain JavaScript is in decline, more and more of them switching to Mocha.
– Asynchronous testing is rather difficult.
– Test files must have specific suffix to run correctly – spec.js by default.

And last but not least the Mocha.
Mocha has been specially built for testing NodeJS modules. Released in 2012 it has a nice start, and JavaScript users are starting to prefer it above other test frameworks, including because it’s to say, somewhat easy to switch from Jasmine to Mocha. It has baked in its own runner unlike Jasmine, plus it works rather nice with asynchronous testing.
Good:
– Simple setup with npm.
– Allows usage of any assertion libraries that will throw exception on failure, such as Chai or Should.
– Lots of plugins, highly extensible.
– Asynchronous testing is easy with method done(). If you use it, the test won’t pass until the done() is called or until it reaches the end of test method.
– Has aliases for functions to be more BDD or TDD-oriented.
Not Good:
– Newest, so there is a lack of support for it for now.
– There are some holes to be patched in near future, but they are working on it.

All in all:
Currently the Mocha is our best choice and we are using it for plain JavaScript testing with NodeJS, and it has proven helpful on many levels. It has also nice and funny reporting tools, including generating HTML files from test reports. This will be explained in some other topic.

Thank you for reading this, for any questions please contact us.

Similar Posts from the author: