javascriptintegration-testingava

Testing code that uses process.on('unhandledRejection') with AVA


How do I use AVA to test a library that registers an unhandledRejection callback:

  process.on('unhandledRejection', e =>
    // do something effectful
  );

In this environment, unhandled rejections are not necessarily errors in the library. I want to ensure that my library is robust against users' callbacks causing an unhandled rejection.

Without a way to do this, I have to use a different testing framework (like Tape) to execute those tests.


Solution

  • AVA will consider unhandled rejections in test files to be a failure. You'd have to run your code "elsewhere", being a child process or worker thread.


    I want to ensure that my library is robust against users' callbacks causing an unhandled rejection.

    IMHO that's your users problem. If your library calls user provided code and you're awaiting a promise return value you could try / catch that error, but arguably letting the program crash is perfectly valid.