NodeJS - 在 process.on 回调中玩笑单元测试 setTimeout [英] NodeJS - Jest unit test setTimeout in process.on callback

查看:44
本文介绍了NodeJS - 在 process.on 回调中玩笑单元测试 setTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 process.on('SIGTERM') 回调中使用 Jest 对计时器进行单元测试,但它似乎从未被调用过.我正在使用 jest.useFakeTimers(),虽然它似乎在一定程度上模拟了 setTimeout 调用,但它并没有在 setTimeout.mock 中结束 对象检查时.

I am trying to unit test a timer using Jest in my process.on('SIGTERM') callback but it never seems to be called. I am using jest.useFakeTimers() and while it does seem to mock the setTimeout call to an extent, it doesn't end up in the setTimeout.mock object when checking it.

我的 index.js 文件:

My index.js file:

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并且控制台日志输出是:

Expected value to be (using ===): 2 Received: 1 and the console log output is:

console.log tmp/index.js:10
    Timer 2 was run

  console.log tmp/index.js:2
    Got SIGTERM

如何让 setTimeout 在此处运行?

How do I get the setTimeout to be run here?

推荐答案

可以做的是模拟进程 on 方法以确保您的处理程序将在 kill 时被调用 方法.

What can be possible done, is to mock the processes on method to make sure your handler will be called on kill method.

确保处理程序被调用的一种方法是在 on 旁边模拟 kill.

One way to make sure the handler will be called is to mock kill alongside on.

describe('Test process SIGTERM handler', () => {
    test.only('runs timeout', () => {
        jest.useFakeTimers();

        processEvents = {};

        process.on = jest.fn((signal, cb) => {
          processEvents[signal] = cb;
        });

        process.kill = jest.fn((pid, signal) => {
            processEvents[signal]();
        });

        require('./index.js');

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

        jest.runAllTimers();

        expect(setTimeout.mock.calls.length).toBe(2);
    });
});

另一种更通用的方法是在 setTimeout 中模拟处理程序,并进行如下调用的测试:

Other way, a more general one, is to mock the handler within a setTimeout and test that is has been called like following:

index.js

var handlers = require('./handlers');

process.on('SIGTERM', () => {
    console.log('Got SIGTERM');
    setTimeout(handlers.someFunction, 300);
});

handlers.js

module.exports = {
    someFunction: () => {}
};

index.spec.js

describe('Test process SIGTERM handler', () => {
    test.only('sets someFunction as a SIGTERM handler', () => {
        jest.useFakeTimers();

        process.on = jest.fn((signal, cb) => {
            if (signal === 'SIGTERM') {
                cb();
            }
        });

        var handlerMock = jest.fn();

        jest.setMock('./handlers', {
            someFunction: handlerMock
        });

        require('./index');

        jest.runAllTimers();

        expect(handlerMock).toHaveBeenCalledTimes(1);
    });
});

这篇关于NodeJS - 在 process.on 回调中玩笑单元测试 setTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆