//定时器功能代码//
Function startTimer (event)
{
var session Timeout=
setTimeout (function ()
{
Self.postMessage (
{
Message:'show dialog'
};
);
}, event.duration);
}
如何为此编写测试用例?
参考Jest 中提供的定时器模型
https://jestjs.io/docs/timer-mocks
jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');
test('Timer Function', () => {
const startTimer = require('../startTimer ');
startTimer ();
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), event.duration);
});