-1

// Code for Timer Function//

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

How to write test case for this ?

  • not every function is testable on its own. this mostly works for functions with defined inputs and outputs or class methods within a defined context. a global function with side effects is rather untestable. – Psi Oct 04 '22 at 05:04

1 Answers1

0

Refer Timer mocks available in 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);
});
Anto P V
  • 24
  • 2