Nodeunit: Runtime/thrown errors in test function are _silent_

时间秒杀一切 提交于 2019-12-22 04:36:10

问题


One of the points of using NodeUnit is to write new functions and test them often. Problem is, if one of the tested functions throws an error (including JS runtime errors), the error is not shown to the user. Here is the simplest possible test case: (Note that a.b.c.d will cause a runtime error)

exports.all = {
  one: function( test ){
    test.done();
  },

  two: function( test ){
    as( function( err, res ){
      test.done();
    });
  },
}


function as( callback ){
  process.nextTick( function() {
    a = testMe();
    callback( null, a );
  });
}

function testMe(){
  a.b.c.d.e = 100;
  return 10;
}

However, testMe() might be a new function I am developing. An uninitialised variable, or anything, will just fall silent.


回答1:


Add your own uncaught exception handling to your test file either via the uncaughtException event:

process.on('uncaughtException', function(err) {
  console.error(err.stack);
});

Or by creating a domain and adding process to it (or whatever event emitter(s) your tests use) so that you can catch the errors that occur within your use of process.nextTick:

var dom = require('domain').create();
dom.add(process);
dom.on('error', function(err) {
    console.error(err.stack);
});

Either way, you'll then get console output that looks like:

ReferenceError: a is not defined
    at testMe (/home/test/test.js:24:3)
    at /home/test/test.js:18:9
    at process._tickDomainCallback (node.js:459:13)


来源:https://stackoverflow.com/questions/19971713/nodeunit-runtime-thrown-errors-in-test-function-are-silent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!