Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expecting promised to resolve or reject doesn't properly fail a test with mocha and chai-as-promised

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?

like image 302
ewok Avatar asked Dec 18 '17 16:12

ewok


2 Answers

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.

like image 117
Luke Stoward Avatar answered Sep 20 '22 00:09

Luke Stoward


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

like image 44
nitya wijayanti Avatar answered Sep 22 '22 00:09

nitya wijayanti