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?
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.
Alternatively with chai-as-promised library, you can try using chai.should and call .notify to indicate its 'done'. 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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With