简体   繁体   English

摩卡测试套件因未捕获的异常而停止

[英]Mocha test suite halted on uncaught exception

I can't prevent an Uncaught Async Exception from halting a suite of mocha tests. 我无法阻止Uncaught Async Exception停止一组mocha测试。 It seems like it should fail just the test, but the mocha process exits. 看起来它应该只是测试失败,但mocha进程退出。 For the given test cases: 对于给定的测试用例:

describe('AsyncWow', () => {

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

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

});

Produces terminal output: 产生终端输出:

  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)

it is halting because the done() never gets called( as after error, it searches for exception handler, and the next line is never touched), 它正在停止,因为done()永远不会被调用(因为在错误之后,它会搜索异常处理程序,并且永远不会触及下一行),

I would change the code to... 我会将代码更改为......

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();
    });
});

or if you want the error to fail the test... 或者如果您希望错误导致测试失败......

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();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM