title: how to test RxJs debounced epics?
body: <p>I'm trying to write a test function to a redux-observable epic. The epic works fine inside the app and I can check that it correctly debounces the actions and waits 300ms before emitting. But for some reason while I'm trying to test it with jest the debounce operator triggers immediately. So my test case to ensure that the debounce is working fails.</p>
  
  <p>This is the test case</p>
  
  <pre class="lang-js prettyprint-override"><code>it('shall not invoke the movies service neither dispatch stuff if the we invoke before 300ms', done =&gt; {
      const $action = ActionsObservable.of(moviesActions.loadMovies('rambo'));
  
      loadMoviesEpic($action).subscribe(actual =&gt; {
          throw new Error('Should not have been invoked');
      });
  
      setTimeout(() =&gt; {
          expect(spy).toHaveBeenCalledTimes(0);
          done();
      }, 200);
  
  });
  </code></pre>
  
  <p>this is my spy definition.</p>
  
  <pre class="lang-js prettyprint-override"><code>jest.mock('services/moviesService');
  
  const spy = jest.spyOn(moviesService, 'searchMovies');
  
  beforeEach(() =&gt; {
      moviesService.searchMovies.mockImplementation(keyword =&gt; {
          return Promise.resolve(moviesResult);
      });
      spy.mockClear();
  });
  </code></pre>
  
  <p>and this is the epic</p>
  
  <pre class="lang-js prettyprint-override"><code>import { Observable, interval } from 'rxjs';
  import { combineEpics } from 'redux-observable';
  
  import 'rxjs/add/operator/switchMap';
  import 'rxjs/add/operator/map';
  import 'rxjs/add/operator/debounce';
  import 'rxjs/add/operator/filter';
  import 'rxjs/add/operator/catch';
  import 'rxjs/add/observable/from';
  import 'rxjs/add/observable/of';
  import 'rxjs/add/observable/interval';
  import 'rxjs/add/observable/concat';
  
  import actionTypes from 'actions/actionTypes';
  import * as moviesService from 'services/moviesService';
  import * as moviesActions from 'actions/moviesActions';
  
  const DEBOUNCE_INTERVAL_IN_MS = 300;
  const MIN_MOVIES_SEARCH_LENGTH = 3;
  
  export function loadMoviesEpic($action) {
    return $action
      .ofType(actionTypes.MOVIES.LOAD_MOVIES)
      .debounce(() =&gt; Observable.interval(DEBOUNCE_INTERVAL_IN_MS))
      .filter(({ payload }) =&gt; payload.length &gt;= MIN_MOVIES_SEARCH_LENGTH)
      .switchMap(({ payload }) =&gt; {
        const loadingAction = Observable.of(moviesActions.loadingMovies());
  
        const moviesResultAction = Observable.from(
          moviesService.searchMovies(payload)
        )
          .map(moviesResultList =&gt; moviesActions.moviesLoaded(moviesResultList))
          .catch(err =&gt; Observable.of(moviesActions.loadError(err)));
  
        return Observable.concat(loadingAction, moviesResultAction);
      });
  }
  
  const rootEpic = combineEpics(loadMoviesEpic);
  
  export default rootEpic;
  </code></pre>
  
  <p>so basically this thing shall not be called, because the debounce time is 300ms and I'm trying to check the spy after 200ms. But after 10ms the spy is being invoked. </p>
  
  <p><strong>How can I properly test this epic? I accept any suggestion but preferably I would like to avoid marble testing and rely only on timers and fake timers.</strong></p>
  
  <p>Thanks :D</p>