Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert's isFulfilled isRejected not working #216

Open
Cazra opened this issue Aug 29, 2017 · 5 comments
Open

assert's isFulfilled isRejected not working #216

Cazra opened this issue Aug 29, 2017 · 5 comments

Comments

@Cazra
Copy link

Cazra commented Aug 29, 2017

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.

@ghost
Copy link

ghost commented Sep 8, 2017

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

@meeber
Copy link
Contributor

meeber commented Sep 9, 2017

@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

@jakobrosenberg
Copy link

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
Copy link

Solved it by adding await

await assert.isRejected(...

@SamuelToh
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants