I can't seem to keep track of setTimeout and clearTimeout in Jasmine tests that go through karma.
I tried options on all of these
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(); // and no
In each case, I can confirm that setTimeout and clearTimeout were called in runMyCode , but instead I always get Expected spy setTimeout to have been called.
For window , obviously, this is because the test and the runner (the Karma window) are in different frames (so why should I expect something else). But because of this, I see no way to confirm that these global functions have been called.
I know that I can use jasmine.clock() to confirm that the timeout / interval callback is being called, but it looks like I can't watch setTimeout myself. And confirmation that clearTimeout was called simply is not possible.
At this point, I can only add a separate abstraction layer to wrap setTimeout and clearTimeout or add functions as dependencies that I did before, but I think this is weird.
source share