-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexit-process.int.test.ts
More file actions
118 lines (93 loc) · 3.73 KB
/
Copy pathexit-process.int.test.ts
File metadata and controls
118 lines (93 loc) · 3.73 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
import process from 'node:process';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { SIGNAL_EXIT_CODES, installExitHandlers } from './exit-process.js';
describe('installExitHandlers', () => {
const onError = 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({ onError, 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 onError with error and kind for uncaughtException', () => {
expect(() => installExitHandlers({ onError })).not.toThrow();
const testError = new Error('Test uncaught exception');
(process as any).emit('uncaughtException', testError);
expect(onError).toHaveBeenCalledWith(testError, 'uncaughtException');
expect(onError).toHaveBeenCalledTimes(1);
expect(onClose).not.toHaveBeenCalled();
});
it('should call onError with reason and kind for unhandledRejection', () => {
expect(() => installExitHandlers({ onError })).not.toThrow();
const testReason = 'Test unhandled rejection';
(process as any).emit('unhandledRejection', testReason);
expect(onError).toHaveBeenCalledWith(testReason, 'unhandledRejection');
expect(onError).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(SIGNAL_EXIT_CODES().SIGINT, {
kind: 'signal',
signal: 'SIGINT',
});
expect(onError).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(SIGNAL_EXIT_CODES().SIGTERM, {
kind: 'signal',
signal: 'SIGTERM',
});
expect(onError).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(SIGNAL_EXIT_CODES().SIGQUIT, {
kind: 'signal',
signal: 'SIGQUIT',
});
expect(onError).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(undefined, { kind: 'exit' });
expect(onError).not.toHaveBeenCalled();
expect(processExitSpy).not.toHaveBeenCalled();
});
});