I'm having a look at the docs of node.js: process event exit.
Emitted when the process is about to exit. There is no way to prevent the exiting of the event loop at this point, and once all exit listeners have finished running the process will exit. Therefore you must only perform synchronous operations in this handler. This is a good hook to perform checks on the module's state (like for unit tests). The callback takes one argument, the code the process is exiting with.
Basic usage looks like this:
process.on('exit', function(code) {
console.log('About to exit with code:', code);
});
Despite the info in docs I can't think of a real life example where I'd like to do some unit tests inside of the callback.
I'd like to make my app as robust as possible. What do you use the exit event for?
I use this event in my app as a last ditch chance to save state to disk in my app and to put some hardware my app is controlling in a known and safe state before my app shuts down. It works as a backstop so that no matter what causes my app to die, I still get a chance to clean up some things.
So, when this event is triggered, I check if there is any unsaved state and, if so, I write it to disk using synchronous I/O (you can't successfully use async operations in this event). Then, I turn off some hardware that my app is controlling.
I don't think it means that you should run a unit test from this callback, but that a running unit test might use this callback to check whether the exit was triggered as expected, or to tell on exit that the unit failed.