我已经看到很多关于测试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 回答
你需要退回承诺并测试它被chai-as-promise所拒绝
应该风格:
要么
return
很重要