使用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.
更多推荐
发布评论