Possible to tell nodeunit not to finish a particular test until test.done() is called?


I am doing some async testing with nodeunit and I was wondering whether it is possible to tell nodeunit to not terminate test cases until test.done is called.

Basically this is how my test cases looks like right now:

exports.basic = testCase({

  setUp: function (callback) {
    this.ws = new WrappedServer();

  tearDown: function (callback) {

  testFoo: function(test) { 
    var socket = ioClient.connect(URL);
    socket.emit('PING', 1, 1);
    socket.on('PONG', function() { 
      // do some assertion of course

The problem now is that PONG is not sent back quick enough for the test code to be executed. Any ideas?

Problem courtesy of: disappearedng


I just had a very similar problem, hence I was browsing this question. In my case the server (analogous to your WrappedServer) was throwing an exception, causing the test to exit abruptly without hitting my event handler with test.done(). I think it is rather rude of nodeunit to swallow the exception without a peep.

I had to resort to the debugger to find the problem, which if you haven't done before, I can save you a web search: node --debug-brk node_modules/nodeunit/bin/nodeunit your_nodeunit_test.js

Solution courtesy of: 2wav


When nodeunit says "Undone tests", that means that the node process has exited without finishing all of the tests. To be clear, that doesn't mean that "PONG is not sent back fast enough", what it means is that there were no more handlers in the event loop. If there are no more handlers, then there is nowhere for a PONG event to come from, so it is not possible for the test to continue.

For example, if you ran something like this:

var s = require('http').createServer();

When you run listen, the server starts listening for incoming data, and is added to the event loop to check for incoming connections. If you only did createServer then no events will fire and your program will just exit.

Do you have a anything bound to an error event anywhere that might be making errors not show up?

Discussion courtesy of: loganfsmyth

The problem is that nodeunit is not expecting any assertions, so it doesn't wait for them and terminates immediately. Count your assertions and call test.expect() in the beginning of the test.

exports.example = function(test) {
    // If you delete next line, the test will terminate immediately with failure.

    }, 5000);       
Discussion courtesy of: Strix

You probably want something like:

/** Invoke a child function safely and prevent nodeunit from swallowing errors */
var safe = function(test, x) {
  try { x(test); } catch(ex) {
    test.ok(false, 'Error invoking async code');

exports.testSomething = function(test){
  test.expect(1); // Prevent early exit
  safe(test, function(test) {
    // ... some async code here
Discussion courtesy of: Doug

This recipe can be found in it's original form on Stack Over Flow.