node.js how to get better error messages for async tests using mocha

后端 未结 2 1180
梦毁少年i
梦毁少年i 2020-12-21 13:51

A typical test in my node.js mocha suite looks like the following:

 it(\"; client should do something\", function(done) {
    var doneFn = function(args) {
          


        
2条回答
  •  春和景丽
    2020-12-21 14:06

    You should to listen to uncaughtException events, in addition to someEvent. This way you'll catch errors happening in client, and those will show up in your test report.

     it("; client should do something", function(done) {
        var doneFn = function(args) {
          // run a bunch of asserts on args
          client.events.removeListener(client.events.someEvent, userMuteFn);
          done();
        }
    
        var failedFn = function(err) {
          client.events.removeListener('uncaughtException', failedFn);
          // propagate client error
          done(err);
        }
        client.events.on(someEvent, doneFn);
    
        client.events.on('uncaughtException', failedFn);
    
        client.triggerEvent();
    });
    

    P.S. In case client is a child process, you also should listen to exit event, which will be triggered in case the child process dies.

提交回复
热议问题