Additional tests for beforeExit event

Extracted from #5583