我正在尝试使用Jest在
我的index.js文件:
我该如何在这里运行
process.on('SIGTERM')
回调中对计时器进行单元测试,但似乎从未被调用。我正在使用jest.useFakeTimers()
,虽然它似乎在某种程度上模拟了setTimeout
调用,但在检查时并没有出现在setTimeout.mock
对象中。我的index.js文件:
process.on('SIGTERM', () => {
console.log('Got SIGTERM');
setTimeout(() => {
console.log('Timer was run');
}, 300);
});
setTimeout(() => {
console.log('Timer 2 was run');
}, 30000);
以及测试文件:
describe('Test process SIGTERM handler', () => {
test.only('runs timeout', () => {
jest.useFakeTimers();
process.exit = jest.fn();
require('./index.js');
process.kill(process.pid, 'SIGTERM');
jest.runAllTimers();
expect(setTimeout.mock.calls.length).toBe(2);
});
});
测试失败:
期望值为(使用 ===): 2 收到的值为: 1 并且控制台日志输出为:
console.log tmp/index.js:10
Timer 2 was run
console.log tmp/index.js:2
Got SIGTERM
我该如何在这里运行
setTimeout
?