jest-mock-process icon indicating copy to clipboard operation
jest-mock-process copied to clipboard

process signals

Open mkg0 opened this issue 5 years ago • 4 comments

Thanks for creating this module!

It would be awesome to have a mock for process.on with signal simulation. Sadly I don't have enough time right now but here is the issue.

mkg0 avatar Aug 26 '20 09:08 mkg0

Hi!

Could you give an example of your request? I'm not entirely sure if you intend to mock the event listener, event emission, or something else.

EpicEric avatar Aug 26 '20 20:08 EpicEric

Alright! 🙂 Then here is my scenario. Below code subscribe to kill signals

  process.on('SIGTERM', async () => {
    // gracefulShutdown
  })
  process.on('SIGINT', async () => {
    // gracefulShutdown
  })

And you can trigger a signal with kill

  process.kill(process.pid, 'SIGINT');

The problem is program determinate when you test the signal even tho you mock the process exit. Would be nice to mock process.on and process.kill and simulate these signals pleasantly.

Here is a simple test example.

const mockProcess = require('jest-mock-process');

const mockedProcessOn = mockProcess.mockProcessOn();
registerSignalHandlers(app) // function to test
mockedProcessOn.sendSignal(process.pid, 'SIGINT')
expect(gracefulShutdown).toHaveBeenCalled();
mockedProcessOn.restoreMock()

// for another scenario
expect(mockedProcessOn).toHaveBeenCalledWith('Kill them all!');

https://nodejs.org/api/process.html#process_process_kill_pid_signal

mkg0 avatar Sep 01 '20 08:09 mkg0

So there's no mockProcessOn right now right? That's just an example.

CMCDragonkai avatar Dec 02 '21 02:12 CMCDragonkai

yes @CMCDragonkai

mkg0 avatar Dec 02 '21 08:12 mkg0