javascript - Spy on setTimeout and clearTimeout in Karma and Jasmine -


i cannot seem able spy on settimeout , cleartimeout in jasmine tests, being run through karma.

i have tried variations on of this

spyon(window, 'settimeout').and.callfake(()=>{}); spyon(global, 'settimeout').and.callfake(()=>{}); spyon(window, 'cleartimeout').and.callthrough();  clock = jasmine.clock(); clock.install(); spyon(clock, 'settimeout').and.callthrough();  runmycode();  expect(window.settimeout).tohavebeencalled(); // no expect(global.settimeout).tohavebeencalled(); // nope expect(window.cleartimeout).tohavebeencalled(); // no again expect(clock.settimeout).tohavebeencalled(); // , no 

in every case, can confirm settimeout , cleartimeout have been invoked in runmycode, instead expected spy settimeout have been called.

for window, because test , runner (the karma window) in different frames (so why should expect different). because of this, can't see way confirm these global functions have been invoked.

i know can use jasmine.clock() confirm timeout/interval callbacks have been invoked, looks can't watch settimeout itself. , confirming cleartimeout has been called isn't possible.

at point, thing can think of add separate layer of abstraction wrap settimeout , cleartimeout or inject functions dependencies, i've done before, think weird.

i able work this:

spyon(window, 'settimeout'); runmycode(); expect(settimeout).tohavebeencalled(); 

just remove 'window' object settimeout call.


Comments