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

Testing uncaughtException and uncaughtRejection handlers #1819

Closed
jcald1 opened this issue May 19, 2020 · 2 comments
Closed

Testing uncaughtException and uncaughtRejection handlers #1819

jcald1 opened this issue May 19, 2020 · 2 comments

Comments

@jcald1
Copy link

jcald1 commented May 19, 2020

How can I tests these two handlers in Jasmine? I don't see a way to disable uncaughtException in Jasmine. I did programmatically remove all uncaughtException in my test using (process.removeListener), set up my uncaughtException handler, and my exception handler caught the exception, but the test still failed in Jasmine.

uncaughtException handler:

const handleUncaughtExceptions = ({ logger }) => {
  process.on('uncaughtException', err => {
    const err2 = error(
      errors.system.UNCAUGHT({
        debug : { originalError : err }
      })
    );
    logger ?  logger.err(err2) : consoleLogIfLoggerNotSetUp(err2);
    throw err2;
  });

  process.on('unhandledRejection', (reason, promise) => {
    const promiseError =
            reason instanceof Error ? reason : new Error('unhandledRejection');
    const err = error(
      errors.system.UNCAUGHT({
        message : 'unhandledRejection',
        debug   : { reason, originalError : promiseError, promise }
      })
    );
    logger ?  logger.err(err) : consoleLogIfLoggerNotSetUp(err);

    if (!process.env.ALLOW_DEBUG || process.env.ALLOW_DEBUG === 'true') {
      throw err;
    }
  });
};

Test

  fit('handles uncaught errors', done => {
    handleUncaughtExceptions({ logger });
    var uncaughExceptionHandlers = process.listeners('uncaughtException');
    let exceptionListener = uncaughExceptionHandlers.pop();
    while (exceptionListener) {
      process.removeListener('uncaughtException', exceptionListener);
      exceptionListener = uncaughExceptionHandlers.pop();
    }

    process.on('uncaughtException', (err)=> {
      err.debug.promise.catch(()=>{});
      console.log(err.debug.promise);
      done();
    });
    Promise.reject(new Error('Rejected Error'));
  });

@sgravrock
Copy link
Member

sgravrock commented Jun 30, 2020

It depends somewhat on what exactly you want to test. I can think of a few possibilities:

  1. The behavior of the error handlers themselves
  2. Whether the error handlers were installed
  3. The integration of the error handlers with other software (possibly including the Node runtime)

For # 1, I suggest exposing the error handlers themselves as named functions and calling them directly from the test. For # 2, you might be able to spy on process.on and assert that it got called as expected. I don't think there's currently a way to accomplish # 3. I'm open to suggestions.

BTW, the reason your test is failing is because it sets up uncaughtException handlers but then triggers an unhandled rejection rather than an uncaught exception. That said, I don't recommend removing Jasmine's handlers like that. The most likely problem that you'd run into is subsequent tests passing when they should have failed. But in general all bets are off once you start tinkering with Jasmine's internals.

@sgravrock
Copy link
Member

I'm closing this due to inactivity. Feel free to open a new issue with more information if you're still having trouble.

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

2 participants