junit 如何用jest为这个函数编写测试用例

cuxqih21  于 2022-11-11  发布在  Jest
关注(0)|答案(1)|浏览(193)

//定时器函数代码//

Function startTimer (event)
    {
       var session Timeout= 
       setTimeout (function ()
   {
    Self.postMessage (
   {
        Message:'show dialog'
   };
   );
   },  event.duration);
    }

如何编写测试用例?

vlju58qv

vlju58qv1#

参考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);
});

相关问题