[英]Test Promise chain ends in .catch (using Mocha / Chai as promised)
I've seen lots of info on testing Promise rejections, but wondering if anyone knows how to write a test that will fail if a promise chain doesn't end with a '.catch'? 我已经看到了很多有关测试Promise拒绝的信息,但是想知道是否有人会写一个如果承诺链不以'.catch'结尾的测试会失败的测试? I'm trying to protect against swallowed errors.
我正在尝试防止吞下错误。
For example, this would pass the test: 例如,这将通过测试:
doSomething() // returns a Promise
.then(doSomethingElse) // returns a Promise
.then(handleResult)
.catch((err) => { console.log(err); }); // logs errors from any rejections
And this would fail: 这将失败:
doSomething() // returns a Promise
.then(doSomethingElse) // returns a Promise
.then(handleResult); // no catch = swallowed errors
I'm using mocha and chai-as-promised. 我正在使用摩卡咖啡和柴如许的。 I'm not using any promise libraries, just native es2015.
我没有使用任何Promise库,只是使用本机es2015。
You need return the promise and test it is rejected with chai-as-promised 您需要退还诺言,并按照承诺的条件测试它是否被拒绝
In should style : 在应风格:
return doSomething()
.then(doSomethingElse)
.then(handleResult).should.be.rejectedWith(Error);
or 要么
return doSomething()
.then(doSomethingElse)
.then(handleResult).should.be.rejected;
return
is important return
很重要
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.