Mocha: How to allowUncaught in Node?

Created on 22 Aug 2016  Â·  8Comments  Â·  Source: mochajs/mocha

How to allowUncaught in Node?
just let the uncaught error stop the debugger.

needs-feedback question

All 8 comments

--allow-uncaught? Would need more information.

@boneskull not working.

hmm, you're right. there's no easy way to do this in Node.js, but I'm not sure why it was designed that way.

UPDATE: You can try a combination of this.runnable().allowUncaught = true and/or process.removeAllListeners('uncaughtException') in a beforeEach(). This will _not_ allow you to use this.skip(), and I'm not sure what happens if you return a Promise. YMMV.

In WebStorm, I can tell the debugger to break on all exceptions, regardless of whether they are caught. Not sure what debugger you're using, but it seems like this behavior should be supported.

I don't know enough about allowUncaught to help you more atm; I _do_ know it's specifically for the browser, but I don't know any more than that.

Also looking for a solution to this

For now:

    let uncaughtExceptionListeners;

    before(() => {
      // Stop Mocha from handling uncaughtExceptions.
      uncaughtExceptionListeners = process.listeners('uncaughtException');
      process.removeAllListeners('uncaughtException');
    });

    after(() => {
      // Resume normal Mocha handling of uncaughtExceptions.
      uncaughtExceptionListeners.forEach((listener) => {
        process.on('uncaughtException', listener);
      });
    });

--allow-uncaught has since been implemented in Node.js, but cannot be toggled on a per-test, hook, or suite basis

Maybe this regressed? Using mocha 6.1.4 in node still having this issue

describe('uncaught', () => {
  it('throw delayed error', () => {
    setTimeout(() => {
      throw new Error('Whoops!')
    }, 1000)
  })
  it('should wait 2 seconds', (done) => {
    setTimeout(done, 2000)
  })
})

Running mocha --allow-uncaught test/unchaught.js

I get

  uncaught
    ✓ throw delayed error
    1) should wait 2 seconds


  1 passing (1s)
  1 failing

  1) uncaught
       should wait 2 seconds:
     Uncaught Error: Whoops!
      at Timeout.setTimeout [as _onTimeout] (test/unchaught.js:5:13)

I am having the same issue despite the --allow-uncaught flag (Mocha 6.2.2). Also, I can reproduce the issue with @aakilfernandes's code.

Was this page helpful?
0 / 5 - 0 ratings