簡體   English   中英

如何在玩笑中為這個 function 編寫測試用例

[英]How to write a test case for this function in jest

// 定時器函數代碼//

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM