chai-as-promised icon indicating copy to clipboard operation
chai-as-promised copied to clipboard

assert's isFulfilled isRejected not working

Open Cazra opened this issue 6 years ago • 5 comments

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.

Cazra avatar Aug 29 '17 14:08 Cazra

+1, why test passes if there error in promise? Looks weird.

ghost avatar Sep 08 '17 16:09 ghost

@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

  1. test This should fail.: AssertionError: expected promise to be fulfilled but it was rejected with undefined

  2. test This should fail.: AssertionError: expected promise to be rejected but it was fulfilled with undefined

meeber avatar Sep 09 '17 12:09 meeber

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

jakobrosenberg avatar Feb 01 '18 09:02 jakobrosenberg

Solved it by adding await

await assert.isRejected(...

jakobrosenberg avatar Feb 01 '18 09:02 jakobrosenberg

I think the docs should be updated to include an example of this?

SamuelToh avatar Jan 11 '23 09:01 SamuelToh