Mocha: UnhandledPromiseRejectionWarning: Unhandled promise rejection

Created on 6 Nov 2017  Â·  6Comments  Â·  Source: mochajs/mocha

When I run tests on functions that returns a rejected promise, the test passes but an UnhandledPromiseRejectionWarning: Unhandled promise rejection is outputted to the console.

const chai = require('chai');
const expect = chai.expect;

function testFunc (opt) {
  if (opt === undefined) {
    return Promise.reject(new Error());
  }
}

describe('testFunc', function () {
  it('return with a promise', function () {
    expect(testFunc()).to.be.a('promise');
  })
  describe('return with a promise that', function () {
    it('is rejected when no parameters are passed to it', async function () {
      try {
        const result = await testFunc();
      } catch (e) { return; }
      return Promise.reject(new Error('Should have thrown'))
    })
  });
});

It produces the following output:

$ npx mocha

  testFunc
    ✓ return with a promise
(node:28519) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error
(node:28519) [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.
    return with a promise that
      ✓ is rejected when no parameters are passed to it

  2 passing (29ms)

I have created a repository at to repreduce this issue.

I believe this will be fixed with #2640 and a similar issue was filed at #2797, but this repository provides an easy, minimalistic way to reproduce the issue.

Most helpful comment

Why was this closed? I get this issue in the latest version of mocha 5.2.0

All 6 comments

I guess calling "reject" on the Promise class itself is not recommended anymore, but I'm not sure why.

@JimTheMan , if that's the case, then it would be nice to give an example on how to 'mock' a rejection, if that's the thing you need to test...

In most cases, having the error is good, because 99% of the time, it's because a return was forgotten, but in the example given (which I also have now encountered), the .reject needs to be called in order to test how rejection is being handled by your code.

EDIT: I solved this by doing the following (if others also encounter this problem)

    $uibModalResult.result = Promise.reject({});
    $uibModalResult.result.catch(_noop); // Makes sure `unhandledPromiseRejection` isn't called...

For the modal, the promise is still rejected, and so we can test what happens upon that still.

Why was this closed? I get this issue in the latest version of mocha 5.2.0

+1 @jwickens , I'm seeing this problem as well.

Maybe it will help somebody, I had the same issue, but because of my own subtle bug:

In a test I forgot to await an async function call, which was throwing an exception. The test wasn't failing, but it was printing this awkward warning.

@d4nyll Can you tell us why this was closed, please?

Was this page helpful?
0 / 5 - 0 ratings