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

UnhandledPromiseRejectionWarning: Unhandled promise rejection #3097

Closed
d4nyll opened this issue Nov 6, 2017 · 6 comments
Closed

UnhandledPromiseRejectionWarning: Unhandled promise rejection #3097

d4nyll opened this issue Nov 6, 2017 · 6 comments

Comments

@d4nyll
Copy link

d4nyll commented Nov 6, 2017

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.

@d4nyll d4nyll closed this as completed Nov 6, 2017
@JimLynchCodes
Copy link

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

@dietergeerts
Copy link

dietergeerts commented Jun 26, 2018

@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.

@jwickens
Copy link

jwickens commented Dec 6, 2018

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

@intelliot
Copy link

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

@CodeSandwich
Copy link

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.

@hovissimo
Copy link

hovissimo commented Dec 16, 2020

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

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

7 participants