首页 文章

测试承诺链在.catch中结束(使用Mocha / Chai承诺)

提问于
浏览
1

我已经看到很多关于测试Promise拒绝的信息,但是想知道如果有人知道怎么写一个测试,如果一个promise链没有以'.catch'结尾会失败?我正在努力防止吞噬错误 .

例如,这将通过测试:

doSomething()                            // returns a Promise
.then(doSomethingElse)                   // returns a Promise
.then(handleResult)
.catch((err) => { console.log(err); });  // logs errors from any rejections

这会失败:

doSomething()                            // returns a Promise
.then(doSomethingElse)                   // returns a Promise
.then(handleResult);                     // no catch = swallowed errors

我正在使用mocha和chai-as-promise . 我没有使用任何承诺库,只是本机es2015 .

1 回答

  • 0

    你需要退回承诺并测试它被chai-as-promise所拒绝

    应该风格:

    return doSomething()                            
    .then(doSomethingElse)                   
    .then(handleResult).should.be.rejectedWith(Error);
    

    要么

    return doSomething()                            
    .then(doSomethingElse)                   
    .then(handleResult).should.be.rejected;
    

    return 很重要

相关问题