简体   繁体   English

期望答应解决或拒绝的尝试无法正确地通过Mocha和Chai-as-Promise的测试失败

[英]expecting promised to resolve or reject doesn't properly fail a test with mocha and chai-as-promised

Using Mocha and chai-as-promised , I'm trying to test that my promised are being resolved and rejected properly. 我正在尝试使用Mochachai-pro-promised ,以确保我的诺言得到了正确解决和拒绝。 But the expect functions given by chai-as-promised aren't properly causing the tests to fail. 但是,chai-promise给出的expect函数不能正确地导致测试失败。 Example: 例:

test.js 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 : 当我执行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? 我如何获得摩卡咖啡以识别故障?

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. return关键字通知mocha等待异步功能解析/拒绝。

Alternatively with chai-as-promised library, you can try using chai.should and call .notify to indicate its 'done'. 另外,对于chai-as-promise库,您可以尝试使用chai.should并调用.notify以指示其“完成”。 Like this: 像这样:

const chai = require('chai');
chai.use(require('chai-as-promised'));
chai.should();

const expect = chai.expect;

describe('foo', () => {
    it('resolve expected', () => {
        expect(new Promise((res, rej) => { res(); })).to.be.fulfilled;
    });
    it('resolve unexpected', (done) => {
        // expect(new Promise((res, rej) => { res(); })).to.be.rejected;
        new Promise((res, rej) => { res(); }).should.be.rejected.and.notify(done);
    });
    it('reject expected', (done) => {
        // expect(new Promise((res, rej) => { rej(); })).to.be.rejected;
        new Promise((res, rej) => { rej(); }).should.be.rejected.and.notify(done);
    });
    it('reject unexpected', (done) => {
        // expect(new Promise((res, rej) => { rej(); })).to.be.fulfilled;
        new Promise((res, rej) => { rej(); }).should.be.fulfilled.and.notify(done);
    });
});

This way mocha will recognize the test failure: 通过这种方式,mocha可以识别测试失败:

foo
    √ resolve expected
    1) resolve unexpected
    √ reject expected
    2) reject unexpected


  2 passing (37ms)   2 failing

  1) foo
       resolve unexpected:
     AssertionError: expected promise to be rejected but it was fulfilled with undefined


  2) foo
       reject unexpected:
     AssertionError: expected promise to be fulfilled but it was rejected with undefined

Check chai-as-promised doc for further reference here 在此处查看chai-promise的文档以获取更多参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM