Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 508
Michael FIG Avatar asked Mar 23 '26 16:03

Michael FIG


1 Answers

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.

like image 125
Mark Wubben Avatar answered Mar 26 '26 04:03

Mark Wubben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!