Unit testing framework for node.js that specifically supports testing async code?

All we need is an easy explanation of the problem, so here it is.

There are 30+ testing frameworks listed on the node.js module list

Which frameworks are most popular? And what are their pros and cons?

I’m looking for a framework that supports unit testing of code that has a lot of async callbacks (probably like most other node projects). Also, I’m looking for something rather simple and small than complex.

So far I’ve looked into:

  • Nodeunit is straightforward and supports async code by letting you define how many assertions you expect to be called and when a test is done.
  • Vowsjs looks interesting. Allows you to nicely structure the tests and comes with many advanced features. Not sure whether these features are actually useful, through, or if they rather stand in your way?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

expresso and vows are the most popular options. Although there are many good options out there. Update: As of Nov./2011, Mocha is the successor to expresso, according to the Readme.md at the expresso repository. should.js is often used with Mocha to allow BDD-style assertions.

Whatever you choose, I recommend using gently to stub core or third party modules.

I use/maintain testosterone, a little testing framework that runs test on serial so you can abuse gently. Not so many people using it though.

Method 2

Since March 2012 we’ve been using Nodeunit and haven’t looked back. It’s straightforward and easy to work with, it lets you choose between module-level and class-level fixtures (setup/teardown), it’s got a reasonable set of built-in assertions, and its command-line tool is very flexible. Try combining it with nodemon, too.

We looked a Vows briefly but didn’t want a whole new weird syntax for defining tests. All you need are assertions, fixtures, and a decent harness, and Nodeunit fits the bill perfectly.

Method 3

QUnit is as simple as a testing framework can get, and support asynchronous calls. It is the testing tool for JQuery, so you can be sure it is highly supported and works well.

You can consider integration with Sinon, which is a mocking framework, which in particular allows you to fake asynchronous events. This tutorial and the other parts of the tutorial linked there show how to use Sinon to test asynchronous code. It is on the browser, but you should be able to adapt it to testing node applications.

Method 4

I haven’t used it yet but what about Jasmine from Pivotal Labs?

Method 5

Minor plug for my own library, but more importantly I just want other harness developers to think about what a async testing in JS should look like:

I wrote specifically OKJS because QUnit was so useless asynchronous. QUinit fails to catch exceptions (!), and requires a hacky start() for keeping track of depth.

Many of the test frameworks out there fail to have a simple callback wrapper for testing timeouts and catching exceptions. I’m not sure why –callbacks are at the heart of asynchronous development. =/

  setTimeout(function() {
    ok( true, "Passed and ready to resume!" );
    start();
  }, 1000);

OKJS catches exceptions in event listeners, and keeps track of outstanding async requests with a interal dead-man’s trigger which can fail the test.

  setTimeout( unit.callback("test callback is fired"), 500);
  setTimeout( unit.callback("callback fired, with subtests", function () {
        unit.assert("in callback, true === true", true,true);
  }),  500);

Would love to see this style support out of the enterprise solutions. Some of the other features OKJS advocates for are test blocks that execute in series (recipe style), but wait between blocks for events to be resolved.

Method 6

test-studio is an npm package that provides a powerful, web based front end for unit testing. It currently only supports mocha and provides things like executing individual or groups of tests and stepping node-inspector into individual tests.

Read more about it here.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply