title: Expect an observable to fail in a test in Angular / Karma / Jasmine
body: <p>I have a component that eventually calls a bunch of HTTP services via successive <code>switchMap</code> to fetch data from backends.</p>
  
  <p>I want to test the error handling part. The app has a global error handling (ErrorHandler) and a local error handling via catchError.</p>
  
  <p>The method to test looks like this one :</p>
  
  <pre><code>  public methodUsingObservable() {
      this.someRequest()
        .pipe(catchError((error) =&gt; this.handleError(error)))
        .subscribe(console.log);
    }
  </code></pre>
  
  <p>Handle error check if the error is linked to the business action, and throw it again to be processed by the global handler</p>
  
  <pre><code>handleError(error) {
      checkLocally(error);
      return throwError(error);
    }
  </code></pre>
  
  <p>For my test, I mock <code>someRequest</code> with <code>throwError</code> to mock an issue with backend call</p>
  
  <pre><code>  it('should handle the error when one occurs', fakeAsync(() =&gt; {
      const service: MachinService = TestBed.get(MachinService);
      spyOn(service,'someRequest').and.returnValue(throwError(new Error("Failing HTTP call")));
      spyOn(service,'handleError').and.callThrough();
  
  
      service.methodUsingObservable();
      tick();
  
      expect(service.methodUsingObservable).toThrowError();
      expect(service.handleError).toHaveBeenCalled();
   }));
  </code></pre>
  
  <p>However, no matter how I do spy and expect on something, Karma keeps considering the test is a failure because the subscription, as actually expected, throw an unhandled error.</p>
  
  <p>I have tried with :</p>
  
  <pre><code>expect(service.methodUsingObservable).toThrowError();
  expect(service.methodUsingObservable()).toThrowError();
  expect(() =&gt; service.methodUsingObservable()).toThrowError();
  </code></pre>
  
  <p>But none seems to teach the system to accept the test as successful, and the test fails.</p>
  
  <pre><code>Chrome 62.0.3202 (Windows 10.0.0) MachinService should handle the error when one occurs FAILED
      Error: Failing HTTP call
          at UserContext.&lt;anonymous&gt; src/app/shared/services/machin.service.spec.ts:12:64)
      ...
  </code></pre>
  
  <p>How can I mark the test as passed in these conditions?</p>