1import { installExitHooks } from '../exit';
2
3it('attaches and removes process listeners', () => {
4  jest.spyOn(process, 'on');
5  jest.spyOn(process, 'removeListener');
6
7  const fn = jest.fn();
8
9  expect(process.on).not.toBeCalled();
10
11  const unsub = installExitHooks(fn);
12  const unsub2 = installExitHooks(jest.fn());
13
14  expect(process.on).toHaveBeenCalledTimes(4);
15  expect(process.on).toHaveBeenNthCalledWith(1, 'SIGHUP', expect.any(Function));
16  expect(process.on).toHaveBeenNthCalledWith(2, 'SIGINT', expect.any(Function));
17  expect(process.on).toHaveBeenNthCalledWith(3, 'SIGTERM', expect.any(Function));
18  expect(process.on).toHaveBeenNthCalledWith(4, 'SIGBREAK', expect.any(Function));
19
20  expect(process.removeListener).not.toBeCalled();
21
22  // Unsub the first listener, this won't remove the other listeners.
23  unsub();
24  expect(process.removeListener).not.toBeCalled();
25
26  // Unsub the second listener, this will remove the other listeners.
27  unsub2();
28  expect(process.removeListener).toBeCalledTimes(4);
29});
30