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