-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathexit-process.unit.test.ts
More file actions
261 lines (210 loc) · 8.22 KB
/
exit-process.unit.test.ts
File metadata and controls
261 lines (210 loc) · 8.22 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import os from 'node:os';
import process from 'node:process';
import { SIGNAL_EXIT_CODES, subscribeProcessExit } from './exit-process.js';
describe('subscribeProcessExit', () => {
const onError = vi.fn();
const onExit = 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(() => subscribeProcessExit({ onError, onExit })).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 onError with error and kind for uncaughtException', () => {
expect(() => subscribeProcessExit({ onError })).not.toThrow();
const testError = new Error('Test uncaught exception');
(process as any).emit('uncaughtException', testError);
expect(onError).toHaveBeenCalledWith(testError, 'uncaughtException');
expect(onError).toHaveBeenCalledOnce();
expect(onExit).not.toHaveBeenCalled();
});
it('should call onError with reason and kind for unhandledRejection', () => {
expect(() => subscribeProcessExit({ onError })).not.toThrow();
const testReason = 'Test unhandled rejection';
(process as any).emit('unhandledRejection', testReason);
expect(onError).toHaveBeenCalledWith(testReason, 'unhandledRejection');
expect(onError).toHaveBeenCalledOnce();
expect(onExit).not.toHaveBeenCalled();
});
it('should call onExit with correct code and reason for SIGINT', () => {
expect(() =>
subscribeProcessExit({ onExit, exitOnSignal: true }),
).not.toThrow();
(process as any).emit('SIGINT');
expect(onExit).toHaveBeenCalledOnce();
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGINT, {
kind: 'signal',
signal: 'SIGINT',
});
expect(onError).not.toHaveBeenCalled();
expect(processExitSpy).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGINT);
});
it('should call onExit with correct code and reason for SIGTERM', () => {
expect(() =>
subscribeProcessExit({ onExit, exitOnSignal: true }),
).not.toThrow();
(process as any).emit('SIGTERM');
expect(onExit).toHaveBeenCalledOnce();
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGTERM, {
kind: 'signal',
signal: 'SIGTERM',
});
expect(onError).not.toHaveBeenCalled();
expect(processExitSpy).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGTERM);
});
it('should call onExit with correct code and reason for SIGQUIT', () => {
expect(() =>
subscribeProcessExit({ onExit, exitOnSignal: true }),
).not.toThrow();
(process as any).emit('SIGQUIT');
expect(onExit).toHaveBeenCalledOnce();
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGQUIT, {
kind: 'signal',
signal: 'SIGQUIT',
});
expect(onError).not.toHaveBeenCalled();
expect(processExitSpy).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGQUIT);
});
it('should not exit process when exitOnSignal is false', () => {
expect(() =>
subscribeProcessExit({ onExit, exitOnSignal: false }),
).not.toThrow();
(process as any).emit('SIGINT');
expect(onExit).toHaveBeenCalledOnce();
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGINT, {
kind: 'signal',
signal: 'SIGINT',
});
expect(onError).not.toHaveBeenCalled();
expect(processExitSpy).not.toHaveBeenCalled();
});
it('should not exit process when exitOnSignal is not set', () => {
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
(process as any).emit('SIGTERM');
expect(onExit).toHaveBeenCalledOnce();
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGTERM, {
kind: 'signal',
signal: 'SIGTERM',
});
expect(onError).not.toHaveBeenCalled();
expect(processExitSpy).not.toHaveBeenCalled();
});
it('should call onExit with exit code and reason for normal exit', () => {
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
const exitCode = 42;
(process as any).emit('exit', exitCode);
expect(onExit).toHaveBeenCalledOnce();
expect(onExit).toHaveBeenCalledWith(exitCode, { kind: 'exit' });
expect(onError).not.toHaveBeenCalled();
expect(processExitSpy).not.toHaveBeenCalled();
});
it('should call onExit with fatal reason when exitOnFatal is true', () => {
expect(() =>
subscribeProcessExit({ onError, onExit, exitOnFatal: true }),
).not.toThrow();
const testError = new Error('Test uncaught exception');
(process as any).emit('uncaughtException', testError);
expect(onError).toHaveBeenCalledWith(testError, 'uncaughtException');
expect(onError).toHaveBeenCalledOnce();
expect(onExit).toHaveBeenCalledWith(1, {
kind: 'fatal',
fatal: 'uncaughtException',
});
expect(onExit).toHaveBeenCalledOnce();
});
it('should use custom fatalExitCode when exitOnFatal is true', () => {
expect(() =>
subscribeProcessExit({
onError,
onExit,
exitOnFatal: true,
fatalExitCode: 42,
}),
).not.toThrow();
const testError = new Error('Test uncaught exception');
(process as any).emit('uncaughtException', testError);
expect(onError).toHaveBeenCalledWith(testError, 'uncaughtException');
expect(onError).toHaveBeenCalledOnce();
expect(onExit).toHaveBeenCalledWith(42, {
kind: 'fatal',
fatal: 'uncaughtException',
});
expect(onExit).toHaveBeenCalledOnce();
});
it('should call onExit with fatal reason for unhandledRejection when exitOnFatal is true', () => {
expect(() =>
subscribeProcessExit({ onError, onExit, exitOnFatal: true }),
).not.toThrow();
const testReason = 'Test unhandled rejection';
(process as any).emit('unhandledRejection', testReason);
expect(onError).toHaveBeenCalledWith(testReason, 'unhandledRejection');
expect(onError).toHaveBeenCalledOnce();
expect(onExit).toHaveBeenCalledWith(1, {
kind: 'fatal',
fatal: 'unhandledRejection',
});
expect(onExit).toHaveBeenCalledOnce();
});
it('should have correct SIGINT exit code on Windows', () => {
const osSpy = vi.spyOn(os, 'platform').mockReturnValue('win32');
const exitCodes = SIGNAL_EXIT_CODES();
expect(exitCodes.SIGINT).toBe(2);
osSpy.mockRestore();
});
it('should have correct SIGINT exit code on Unix-like systems', () => {
const osSpy = vi.spyOn(os, 'platform').mockReturnValue('linux');
const exitCodes = SIGNAL_EXIT_CODES();
expect(exitCodes.SIGINT).toBe(130);
osSpy.mockRestore();
});
it('should calculate Windows exit codes correctly when platform is mocked to Windows', () => {
const osSpy = vi.spyOn(os, 'platform').mockReturnValue('win32');
const exitCodes = SIGNAL_EXIT_CODES();
expect(exitCodes.SIGINT).toBe(2); // SIGINT_CODE = 2 on Windows
expect(exitCodes.SIGTERM).toBe(143); // 128 + 15 = 143
expect(exitCodes.SIGQUIT).toBe(131); // 128 + 3 = 131
osSpy.mockRestore();
});
it('should call onExit only once even when close is called multiple times', () => {
expect(() =>
subscribeProcessExit({ onExit, exitOnSignal: true }),
).not.toThrow();
(process as any).emit('SIGINT');
expect(onExit).toHaveBeenCalledOnce();
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGINT, {
kind: 'signal',
signal: 'SIGINT',
});
onExit.mockClear();
(process as any).emit('SIGTERM');
expect(onExit).not.toHaveBeenCalled();
(process as any).emit('exit', 0);
expect(onExit).not.toHaveBeenCalled();
});
});