|
| 1 | +import process from 'node:process'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { SIGNAL_EXIT_CODES, installExitHandlers } from './exit-process.js'; |
| 4 | + |
| 5 | +describe('installExitHandlers', () => { |
| 6 | + const onError = vi.fn(); |
| 7 | + const onExit = vi.fn(); |
| 8 | + const processOnSpy = vi.spyOn(process, 'on'); |
| 9 | + const processExitSpy = vi.spyOn(process, 'exit').mockImplementation(vi.fn()); |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + vi.clearAllMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + [ |
| 17 | + 'uncaughtException', |
| 18 | + 'unhandledRejection', |
| 19 | + 'SIGINT', |
| 20 | + 'SIGTERM', |
| 21 | + 'SIGQUIT', |
| 22 | + 'exit', |
| 23 | + ].forEach(event => { |
| 24 | + process.removeAllListeners(event); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should install event listeners for all expected events', () => { |
| 29 | + expect(() => installExitHandlers({ onError, onExit })).not.toThrow(); |
| 30 | + |
| 31 | + expect(processOnSpy).toHaveBeenCalledWith( |
| 32 | + 'uncaughtException', |
| 33 | + expect.any(Function), |
| 34 | + ); |
| 35 | + expect(processOnSpy).toHaveBeenCalledWith( |
| 36 | + 'unhandledRejection', |
| 37 | + expect.any(Function), |
| 38 | + ); |
| 39 | + expect(processOnSpy).toHaveBeenCalledWith('SIGINT', expect.any(Function)); |
| 40 | + expect(processOnSpy).toHaveBeenCalledWith('SIGTERM', expect.any(Function)); |
| 41 | + expect(processOnSpy).toHaveBeenCalledWith('SIGQUIT', expect.any(Function)); |
| 42 | + expect(processOnSpy).toHaveBeenCalledWith('exit', expect.any(Function)); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should call onError with error and kind for uncaughtException', () => { |
| 46 | + expect(() => installExitHandlers({ onError })).not.toThrow(); |
| 47 | + |
| 48 | + const testError = new Error('Test uncaught exception'); |
| 49 | + |
| 50 | + (process as any).emit('uncaughtException', testError); |
| 51 | + |
| 52 | + expect(onError).toHaveBeenCalledWith(testError, 'uncaughtException'); |
| 53 | + expect(onError).toHaveBeenCalledTimes(1); |
| 54 | + expect(onExit).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should call onError with reason and kind for unhandledRejection', () => { |
| 58 | + expect(() => installExitHandlers({ onError })).not.toThrow(); |
| 59 | + |
| 60 | + const testReason = 'Test unhandled rejection'; |
| 61 | + |
| 62 | + (process as any).emit('unhandledRejection', testReason); |
| 63 | + |
| 64 | + expect(onError).toHaveBeenCalledWith(testReason, 'unhandledRejection'); |
| 65 | + expect(onError).toHaveBeenCalledTimes(1); |
| 66 | + expect(onExit).not.toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should call onExit and exit with code 0 for SIGINT', () => { |
| 70 | + expect(() => installExitHandlers({ onExit })).not.toThrow(); |
| 71 | + |
| 72 | + (process as any).emit('SIGINT'); |
| 73 | + |
| 74 | + expect(onExit).toHaveBeenCalledTimes(1); |
| 75 | + expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGINT, { |
| 76 | + kind: 'signal', |
| 77 | + signal: 'SIGINT', |
| 78 | + }); |
| 79 | + expect(onError).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should call onExit and exit with code 0 for SIGTERM', () => { |
| 83 | + expect(() => installExitHandlers({ onExit })).not.toThrow(); |
| 84 | + |
| 85 | + (process as any).emit('SIGTERM'); |
| 86 | + |
| 87 | + expect(onExit).toHaveBeenCalledTimes(1); |
| 88 | + expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGTERM, { |
| 89 | + kind: 'signal', |
| 90 | + signal: 'SIGTERM', |
| 91 | + }); |
| 92 | + expect(onError).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should call onExit and exit with code 0 for SIGQUIT', () => { |
| 96 | + expect(() => installExitHandlers({ onExit })).not.toThrow(); |
| 97 | + |
| 98 | + (process as any).emit('SIGQUIT'); |
| 99 | + |
| 100 | + expect(onExit).toHaveBeenCalledTimes(1); |
| 101 | + expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGQUIT, { |
| 102 | + kind: 'signal', |
| 103 | + signal: 'SIGQUIT', |
| 104 | + }); |
| 105 | + expect(onError).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should call onExit for successful process termination with exit code 0', () => { |
| 109 | + expect(() => installExitHandlers({ onExit })).not.toThrow(); |
| 110 | + |
| 111 | + (process as any).emit('exit', 0); |
| 112 | + |
| 113 | + expect(onExit).toHaveBeenCalledTimes(1); |
| 114 | + expect(onExit).toHaveBeenCalledWith(0, { kind: 'exit' }); |
| 115 | + expect(onError).not.toHaveBeenCalled(); |
| 116 | + expect(processExitSpy).not.toHaveBeenCalled(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should call onExit for failed process termination with exit code 1', () => { |
| 120 | + expect(() => installExitHandlers({ onExit })).not.toThrow(); |
| 121 | + |
| 122 | + (process as any).emit('exit', 1); |
| 123 | + |
| 124 | + expect(onExit).toHaveBeenCalledTimes(1); |
| 125 | + expect(onExit).toHaveBeenCalledWith(1, { kind: 'exit' }); |
| 126 | + expect(onError).not.toHaveBeenCalled(); |
| 127 | + expect(processExitSpy).not.toHaveBeenCalled(); |
| 128 | + }); |
| 129 | +}); |
0 commit comments