Additional tests for beforeExit event

Tests extracted from #5583

One test doesn't pass. I think setImmediate is cleared by RUN_NOWAIT, so more becomes 0, so exit occurs prematurely.