-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathexit-process.unit.test.ts
More file actions
109 lines (84 loc) · 3.44 KB
/
exit-process.unit.test.ts
File metadata and controls
109 lines (84 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import process from 'node:process';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { installExitHandlers } from './exit-process.js';
describe('exit-process tests', () => {
const onFatal = vi.fn();
const onClose = vi.fn();
const processOnSpy = vi.spyOn(process, 'on');
const processExitSpy = vi.spyOn(process, 'exit').mockImplementation(vi.fn());
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
[
'uncaughtException',
'unhandledRejection',
'SIGINT',
'SIGTERM',
'SIGQUIT',
'exit',
].forEach(event => {
process.removeAllListeners(event);
});
});
it('should install event listeners for all expected events', () => {
expect(() => installExitHandlers({ onFatal, onClose })).not.toThrow();
expect(processOnSpy).toHaveBeenCalledWith(
'uncaughtException',
expect.any(Function),
);
expect(processOnSpy).toHaveBeenCalledWith(
'unhandledRejection',
expect.any(Function),
);
expect(processOnSpy).toHaveBeenCalledWith('SIGINT', expect.any(Function));
expect(processOnSpy).toHaveBeenCalledWith('SIGTERM', expect.any(Function));
expect(processOnSpy).toHaveBeenCalledWith('SIGQUIT', expect.any(Function));
expect(processOnSpy).toHaveBeenCalledWith('exit', expect.any(Function));
});
it('should call onFatal with error and kind for uncaughtException', () => {
expect(() => installExitHandlers({ onFatal })).not.toThrow();
const testError = new Error('Test uncaught exception');
(process as any).emit('uncaughtException', testError);
expect(onFatal).toHaveBeenCalledWith(testError, 'uncaughtException');
expect(onFatal).toHaveBeenCalledTimes(1);
expect(onClose).not.toHaveBeenCalled();
});
it('should call onFatal with reason and kind for unhandledRejection', () => {
expect(() => installExitHandlers({ onFatal })).not.toThrow();
const testReason = 'Test unhandled rejection';
(process as any).emit('unhandledRejection', testReason);
expect(onFatal).toHaveBeenCalledWith(testReason, 'unhandledRejection');
expect(onFatal).toHaveBeenCalledTimes(1);
expect(onClose).not.toHaveBeenCalled();
});
it('should call onClose and exit with code 0 for SIGINT', () => {
expect(() => installExitHandlers({ onClose })).not.toThrow();
(process as any).emit('SIGINT');
expect(onClose).toHaveBeenCalledTimes(1);
expect(onClose).toHaveBeenCalledWith();
expect(onFatal).not.toHaveBeenCalled();
});
it('should call onClose and exit with code 0 for SIGTERM', () => {
expect(() => installExitHandlers({ onClose })).not.toThrow();
(process as any).emit('SIGTERM');
expect(onClose).toHaveBeenCalledTimes(1);
expect(onClose).toHaveBeenCalledWith();
expect(onFatal).not.toHaveBeenCalled();
});
it('should call onClose and exit with code 0 for SIGQUIT', () => {
expect(() => installExitHandlers({ onClose })).not.toThrow();
(process as any).emit('SIGQUIT');
expect(onClose).toHaveBeenCalledTimes(1);
expect(onClose).toHaveBeenCalledWith();
expect(onFatal).not.toHaveBeenCalled();
});
it('should call onClose for normal exit', () => {
expect(() => installExitHandlers({ onClose })).not.toThrow();
(process as any).emit('exit');
expect(onClose).toHaveBeenCalledTimes(1);
expect(onClose).toHaveBeenCalledWith();
expect(onFatal).not.toHaveBeenCalled();
expect(processExitSpy).not.toHaveBeenCalled();
});
});