title: In Jest, how can I unit test a method that subscribes to an observable
body: <p>I have various methods in my components that subscribe to methods in injected dependencies, that return observables.</p>
  
  <p>I want to write Jest unit tests to ensure that when these observables return / error, my methods do the correct thing.</p>
  
  <p>In the below example I am trying to write a test that checks if <code>doAThing</code> has fired. Neither of the below tests work. They both fail with errors like </p>
  
  <blockquote>
    <p>'returnMyObservable.subscribe is not a function'.</p>
  </blockquote>
  
  <pre><code>// Example method to test component 
  public testFunction (): void {
      this.myService.returnMyObservable.subscribe(
          ( value ) =&gt; this.doAThing( value )
      )
  }
  </code></pre>
  
  <pre class="lang-js prettyprint-override"><code>describe( 'myComponemt', () =&gt; {
  
      let fixture;
      let myServiceMock;
  
      beforeEach( () =&gt; {
          myServiceMock = {
              returnMyObservable: fn()
          }
  
          fixture = new myComponent( myServiceMock );
      });
  
  
      // 1) I have tried mocking with a returned value
      it ( 'should call do a thing when value is returned', () =&gt; {
          myServiceMock.returnMyOnservable.mockReturnValue( true );
  
          fixture.testFunction();
  
          expect( fixture.doAThing ).toHaveBeenCalled();
      });
  
      // 2) I have tried returning an observable
      it ( 'should call do a thing when value is returned', () =&gt; {
          myServiceMock.returnMyOnservable.mockReturnValue( of( true ) );
  
          fixture.testFunction();
  
          expect( fixture.doAThing ).toHaveBeenCalled();
      });
  
  });
  </code></pre>