简体   繁体   English

Test Promise链以.catch结尾(按承诺使用Mocha / Chai)

[英]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.

相关问题 如何使用mocha / chai / chai-as-promised测试ES7异步函数 - How to test an ES7 async function using mocha/chai/chai-as-promised 在测试 promise 拒绝时,在将 Mocha 与 chai-as-promised 一起使用时获得“不是一个 thenable” - When testing for promise rejections, getting “is not a thenable” when using Mocha with chai-as-promised 期望答应解决或拒绝的尝试无法正确地通过Mocha和Chai-as-Promise的测试失败 - expecting promised to resolve or reject doesn't properly fail a test with mocha and chai-as-promised 按照承诺用 Chai 测试拒绝 - Test a rejection with Chai as promised 与摩卡咖啡和蔡测试,看诺言是否已解决或被拒绝 - Test with mocha and chai to see if a promise is resolved or rejected 柴如诺,不等诺言兑现 - Chai as promised not waiting for promise to be fulfilled 使用 Chai 和 mocha 的测试用例 - Test cases using Chai and mocha 测试承诺是否按照承诺使用 Mocha + Chai 抛出/拒绝似乎不起作用 - Testing if a promise throws/rejected with Mocha + Chai as promised doesn't seem to be working Mocha和Chai-as-promise超时 - Mocha and Chai-as-promised time out 为什么这个chai-as-promised AssertionError打印到Console而不是我的Mocha测试运行器? - Why is this chai-as-promised AssertionError printing to the Console but not my Mocha test runner?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM