期望承诺解决或拒绝没有适当地失败测试与摩卡和chai

编程入门 行业动态 更新时间:2024-10-20 06:26:47
期望承诺解决或拒绝没有适当地失败测试与摩卡和chai-as-promise(expecting promised to resolve or reject doesn't properly fail a test with mocha and chai-as-promised)

使用Mocha和chai-as-promise ,我试图测试我的承诺正在被解决并被正确拒绝。 但是chai-as-promise所给出的expect函数并没有正确地导致测试失败。 例:

test.js

const chai = require('chai') chai.use(require('chai-as-promised')) const expect = chai.expect describe('foo', () => { it('resolve expected', () => { expect(new Promise((res,rej) => {res()})).to.be.fulfilled }) it('resolve unexpected', () => { expect(new Promise((res,rej) => {res()})).to.be.rejected }) it('reject expected', () => { expect(new Promise((res,rej) => {rej()})).to.be.rejected }) it('reject unexpected', () => { expect(new Promise((res,rej) => {rej()})).to.be.fulfilled }) })

当我执行mocha test.js :

foo ✓ resolve expected ✓ resolve unexpected (node:2659) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError: expected promise to be rejected but it was fulfilled with undefined (node:2659) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. ✓ reject expected ✓ reject unexpected (node:2659) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): AssertionError: expected promise to be fulfilled but it was rejected with undefined 4 passing (10ms)

您可以看到断言错误似乎被抛出,但摩卡不会接受它们。 如何让摩卡识别失败?

Using Mocha and chai-as-promised, I'm trying to test that my promised are being resolved and rejected properly. But the expect functions given by chai-as-promised aren't properly causing the tests to fail. Example:

test.js

const chai = require('chai') chai.use(require('chai-as-promised')) const expect = chai.expect describe('foo', () => { it('resolve expected', () => { expect(new Promise((res,rej) => {res()})).to.be.fulfilled }) it('resolve unexpected', () => { expect(new Promise((res,rej) => {res()})).to.be.rejected }) it('reject expected', () => { expect(new Promise((res,rej) => {rej()})).to.be.rejected }) it('reject unexpected', () => { expect(new Promise((res,rej) => {rej()})).to.be.fulfilled }) })

When I execute mocha test.js:

foo ✓ resolve expected ✓ resolve unexpected (node:2659) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError: expected promise to be rejected but it was fulfilled with undefined (node:2659) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. ✓ reject expected ✓ reject unexpected (node:2659) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): AssertionError: expected promise to be fulfilled but it was rejected with undefined 4 passing (10ms)

You can see the assertion-errors seem to get thrown, but mocha doesn't pick them up. How can I get mocha to recognize the failures?

最满意答案

正如我们确认的那样,并且为了将来参考,问题是您没有在每个测试中返回断言。

it('reject expected', () => { return expect(new Promise((res,rej) => {rej()})).to.be.rejected }) it('reject unexpected', () => { return expect(new Promise((res,rej) => {rej()})).to.be.fulfilled })

这就是为什么测试通过但后来在它最终返回时打印出一个“未处理”的承诺。 return关键字通知mocha等待异步函数解析/拒绝。

As we confirmed, and for future reference, the problem was that you did not return the assertion in each test.

it('reject expected', () => { return expect(new Promise((res,rej) => {rej()})).to.be.rejected }) it('reject unexpected', () => { return expect(new Promise((res,rej) => {rej()})).to.be.fulfilled })

This is why the tests passed but then later printed an "unhandled" promise when it did finally return. The return keyword informs mocha to wait for the async function to resolve/reject.

更多推荐

本文发布于:2023-07-23 18:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:摩卡   测试   chai

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!