5

I had listened on 'SIGTERM' signal to do a graceful shutdown:

   // snippet of server.js 
   process.on('SIGTERM', () => {
        console.log('I will close database here.')
        process.exit(0)
   })

I used Jest to do the test, here is code below:

describe('on SIGTERM', () => {
 beforeAll(() => {
     process.exit = jest.fn(); // substituted by mock function
     return require('../server'); // run server.js
 });

 it('should exit with code 0', async () => {
     process.kill(process.pid, 'SIGTERM'); // the code killed my jest process immediately
     expect(process.exit).toHaveBeenCalledWith(0);
 })
})

Here is the error message below:

npm run test:server server/tests/server.spec.js

error Command failed with exit code 143.

I was sure that process.exit was substituted by mock function because I had tried call process.exit(0), the process is keeping alive. But process.kill killed my process immediately.

Why does this happen?

How can I do my test correctly? ( I wanna test the process.on('SIGTERM'))

Community
  • 1
  • 1
Erum Huang
  • 307
  • 1
  • 3
  • 16

0 Answers0