Eru*_*ang 5 javascript node.js jestjs
我听过 'SIGTERM' 信号进行正常关闭:
// snippet of server.js
process.on('SIGTERM', () => {
console.log('I will close database here.')
process.exit(0)
})
Run Code Online (Sandbox Code Playgroud)
我使用 Jest 进行测试,代码如下:
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);
})
})
Run Code Online (Sandbox Code Playgroud)
这是下面的错误消息:
npm 运行测试:服务器服务器/测试/server.spec.js
错误 命令失败,退出代码 143。
我确信 process.exit 被模拟函数替换了,因为我曾尝试调用 process.exit(0),该进程保持活动状态。但是 process.kill 立即杀死了我的进程。
为什么会发生这种情况?
如何正确进行测试?(我想测试 process.on('SIGTERM'))