Skip to content

Instantly share code, notes, and snippets.

@stewx
stewx / setFakeTimer.ts
Last active March 29, 2019 13:12
Mock rxjs 5 Observable.timer for Angular unit tests with Jasmine
export function setFakeTimer(maxFrames?: number): TestScheduler {
const originalTimer = Observable.timer;
const scheduler = new TestScheduler((a, b) => expect(a).toEqual(b));
if (maxFrames) {
scheduler.maxFrames = maxFrames;
}
spyOn(Observable, 'timer').and.callFake((initialDelay: number, dueTime: number) => {
return originalTimer.call(this, initialDelay, dueTime, scheduler);
});