【问题标题】:Mocha test suite halted on uncaught exceptionMocha 测试套件因未捕获的异常而停止
【发布时间】:2015-03-04 04:12:30
【问题描述】:

我无法阻止 Uncaught Async Exception 停止一组 mocha 测试。看起来它应该只是测试失败,但是 mocha 进程退出了。对于给定的测试用例:

describe('AsyncWow', () => {

    it('should proceeed after this test', (done) => {
        setTimeout(() => {
            throw new Error('boom');
            done();
        });
    });

    it('but never gets here', (done) => {
        done();
    });

});

产生终端输出:

  AsyncWow

/source/test/foo.test.js:6
         throw new Error('boom');
               ^
Error: boom
    at [object Object]._onTimeout (/source/test/foo.test.js:6:16)
    at Timer.listOnTimeout (timers.js:110:15)

【问题讨论】:

    标签: javascript node.js testing mocha.js


    【解决方案1】:

    它正在停止,因为done() 永远不会被调用(在错误之后,它会搜索异常处理程序,并且永远不会触及下一行),

    我会将代码更改为...

    describe('AsyncWow', () => {
        it('should proceeed after this test', (done) => {
            setTimeout(() => {
                try{
                    throw new Error('boom');
                    done(new Error('This line should not be reached.'));
                }catch(e){
                    done(); 
                }
    
            });
        });
        it('but never gets here', (done) => {
            done();
        });
    });
    

    或者如果您希望错误无法通过测试...

    describe('AsyncWow', () => {
        it('should proceeed after this test', (done) => {
            setTimeout(() => {
                try{
                    throw new Error('boom');
                    done();
                }catch(e){
                    done(e); 
                }
    
            });
        });
        it('but never gets here', (done) => {
            done();
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      • 2017-07-02
      相关资源
      最近更新 更多