chai-as-promised
chai-as-promised copied to clipboard
assert's isFulfilled isRejected not working
I'm having trouble with isFulfilled and isRejected always passing. I'm using chai-as-promised with asserts in Mocha.
isFulfilled example:
describe('test', () => {
it('This should fail.', () => {
return assert.isFulfilled(Promise.reject());
});
});
This passes, but prints the following to the console:
(node:26583) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): AssertionError: expected promise to be fulfilled but it was rejected with undefined
isRejected example:
describe('test', () => {
it('This should fail.', () => {
return assert.isRejected(Promise.resolve());
});
});
This also passes, but prints the following to the console:
(node:26674) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError: expected promise to be rejected but it was fulfilled with undefined
I've confirmed that this isn't just something wrong with Mocha. When I do a test like the following:
describe('test', () => {
it('This should fail.', () => {
return Promise.reject();
});
});
the test fails as expected.
+1, why test passes if there error in promise? Looks weird.
@Cazra What versions of node, chai, chai-as-promised, and mocha are you using? What does your entire test file look like?
I just performed a test using node 8.4.0, chai 4.1.2, chai-as-promised 7.1.1, mocha 3.5.0, and this code:
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const assert = chai.assert;
describe('test', () => {
it('This should fail.', () => {
return assert.isFulfilled(Promise.reject());
});
it('This should fail.', () => {
return assert.isRejected(Promise.resolve());
});
});
And the output was:
test 1) This should fail. 2) This should fail.
0 passing (13ms) 2 failing
test This should fail.: AssertionError: expected promise to be fulfilled but it was rejected with undefined
test This should fail.: AssertionError: expected promise to be rejected but it was fulfilled with undefined
I have the exact same issue.
assert.isRejected(Promise.resolve('foobar'))
passes, but prints
(node:32876) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 17): AssertionError: expected promise to be rejected but it was fulfilled with 'foobar'
Node 8.7.0 chai 4.1.2 chai-as-promised 7.1.1 mocha 5.0.0
Solved it by adding await
await assert.isRejected(...
I think the docs should be updated to include an example of this?