nodeunit icon indicating copy to clipboard operation
nodeunit copied to clipboard

Wrap tests in domain instead of try/catch

Open Floby opened this issue 12 years ago • 0 comments

In several cases, async errors with nodeunit and streams2 only result in the test giving a "test not run or test.done not called". without any sign of the error. What I ended up doing is something like this

Function.prototype.withDomain = function(withStack) {
  var fn = this;
  return function(test) {
    var d = domain.create();
    d.on('error', function(e) {
      test.fail('test failed with ' + e.message);
      if(withStack) {
        console.error(e.stack)
      }
      test.done();
    });
    d.run(fn.bind(this, test));
  }
}

exports['my test'] = function (test) {
  setTimeout(function() {
    throw new Error('Throwing an error asynchronously');
    test.done()
  });
}.withDomain()

I just thought this was useful enough to be the default.

Floby avatar Jul 08 '13 07:07 Floby