-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathexit-process.int.test.ts
More file actions
128 lines (100 loc) · 4.05 KB
/
exit-process.int.test.ts
File metadata and controls
128 lines (100 loc) · 4.05 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
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 and exit with code 0 for SIGINT', () => {
expect(() => subscribeProcessExit({ onExit })).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();
});
it('should call onExit and exit with code 0 for SIGTERM', () => {
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();
});
it('should call onExit and exit with code 0 for SIGQUIT', () => {
expect(() => subscribeProcessExit({ onExit })).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();
});
it('should call onExit for successful process termination with exit code 0', () => {
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
(process as any).emit('exit', 0);
expect(onExit).toHaveBeenCalledOnce();
expect(onExit).toHaveBeenCalledWith(0, { kind: 'exit' });
expect(onError).not.toHaveBeenCalled();
expect(processExitSpy).not.toHaveBeenCalled();
});
it('should call onExit for failed process termination with exit code 1', () => {
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
(process as any).emit('exit', 1);
expect(onExit).toHaveBeenCalledOnce();
expect(onExit).toHaveBeenCalledWith(1, { kind: 'exit' });
expect(onError).not.toHaveBeenCalled();
expect(processExitSpy).not.toHaveBeenCalled();
});
});