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

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

//定时器函数代码//

  1. Function startTimer (event)
  2. {
  3. var session Timeout=
  4. setTimeout (function ()
  5. {
  6. Self.postMessage (
  7. {
  8. Message:'show dialog'
  9. };
  10. );
  11. }, event.duration);
  12. }

如何编写测试用例?

vlju58qv

vlju58qv1#

参考Jest中提供的计时器模拟
https://jestjs.io/docs/timer-mocks

  1. jest.useFakeTimers();
  2. jest.spyOn(global, 'setTimeout');
  3. test('Timer Function', () => {
  4. const startTimer = require('../startTimer ');
  5. startTimer ();
  6. expect(setTimeout).toHaveBeenCalledTimes(1);
  7. expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), event.duration);
  8. });

相关问题