close

[Solved] Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai Error Occurs?

Today I get the following error Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai in javascript.

How To Solve Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai Error ?

  1. How To Solve Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai Error ?

    To Solve Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai Error Just a heads-up that you can get a UnhandledPromiseRejectionWarning if you accidentally put your test code outside of the it-function. 😬

  2. Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai

    To Solve Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai Error Just a heads-up that you can get a UnhandledPromiseRejectionWarning if you accidentally put your test code outside of the it-function. 😬

Solution 1

I got this error when stubbing with sinon.

The fix is to use npm package sinon-as-promised when resolving or rejecting promises with stubs.

Instead of …

sinon.stub(Database, 'connect').returns(Promise.reject( Error('oops') ))

Use …

require('sinon-as-promised');
sinon.stub(Database, 'connect').rejects(Error('oops'));

Solution 2

Just a heads-up that you can get a UnhandledPromiseRejectionWarning if you accidentally put your test code outside of the it-function. 😬

    describe('My Test', () => {
      context('My Context', () => {
        it('should test something', () => {})
        const result = testSomething()
        assert.isOk(result)
        })
      })

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment