Mocking fromEvent in rxjs6

Spy on fromEvent haven’t been called

spyOn(Observable, 'fromEvent')

Above mentioned piece of code is not working for rxjs6 version but working on rxjs5

Work arounds to make this work but failed

import {fromEvent} from 'rxjs'

let Obj = { observableFromEvent: fromEvent }
spyOn(Obj, 'observableFromEvent')

/* jasmine test case failed with spy hasn't been called */
import * as rxjs form 'rxjs'

spyOn(rxjs, 'fromEvent')

/* Test case failed with no setter or non writable fromEvent */

5 thoughts on “Mocking fromEvent in rxjs6

  1. just use jasmine marbles for that, you can do it like that:

    const myEvents = hot('--a-', { a: event });
    const expected = cold('--b', { b: result });
  2. @hannaraczek Observable.fromEvent() is from RXJS 5, they switched to just fromEvent() in RXJS 6… As this question is specifically for getting RXJS 6 to work, that is not a viable solution here.

  3. I know this is a lame solution but maybe just wrapping fromEvent would be enough (I didn’t test this):

    import {fromEvent as realFromEvent} from 'rxjs'
    it('test', () => {
      let called = false;
      const fromEvent = (...args: any[]) => {
        const o = realFromEvent(...args);
        called = true;
        return o;
      expect(called).toBeTruthy(); // or whatever...