title: HOW DO TESTING ANGULAR - catchError operator 'rxjs'
body: <p><a href="https://i.stack.imgur.com/RDsyi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RDsyi.png" alt="enter image description here" /></a></p>
  <p>can you please help me to test the code. I have not been able to test the catchError operator that is inside the pipe of the observable interval (1000) ...</p>
  <p>I don't know much unit testing in angular for observables of an inteval (1000). I need to simulate the execution of an interval, and simulate the error. I did not do the code, the code was already done, I am a new employee and I was assigned the task of doing unit testing. I don't know if it is necessary to catchError on an interval.</p>
  <p>My file ts is:</p>
  <pre><code>import { Injectable, EventEmitter } from '@angular/core';
  import {Subscription, Observable, interval, BehaviorSubject, throwError} from 'rxjs';
  import {take, map, catchError, finalize} from 'rxjs/operators';
  import { environment } from 'environments/environment';
  
  /**
   * @Class CounterSessionProvider
   * @description This class is a provider for control session time
   * @author Andres Giraldo Londoño, Pragma S.A.
   */
  @Injectable({
    providedIn: 'root',
  })
  export class CounterSessionProvider {
    // 4 minutes
    public inactivityTime = 20; // environment.inactivityTime; // environment.inactivityTime -&gt; 20 seconds
    counterStateEmitter = new EventEmitter();
    currentSubscription: Subscription;
    public substracCounter$: BehaviorSubject&lt;boolean&gt;;
  
    constructor() {
      this.substracCounter$ = new BehaviorSubject(false);
    }
  
    public start() {
      this.currentSubscription = this.initInterval().subscribe();
    }
  
    public initInterval(): Observable&lt;number&gt; {
      return interval(1000).pipe(
        take(this.inactivityTime),
        map((index: number) =&gt; this.inactivityTime - (index + 1)),
        catchError(err =&gt; {
          this.counterStateEmitter.error(err);
          return throwError(err);
        })
      );
    }
  
    public restarCounterTime(): void {
      this.substracCounter$.next(true);
    }
  
    public stop() {
      if (this.currentSubscription) {
        this.currentSubscription.unsubscribe();
      }
      this.counterStateEmitter.emit('ABORTED');
    }
  }
  </code></pre>
  <p>My file spec.ts is:</p>
  <pre><code>import {fakeAsync, TestBed, tick} from '@angular/core/testing';
  import { CounterSessionProvider } from './counter-session.provider';
  import {interval, Observable, Subscription, throwError} from 'rxjs';
  import {catchError, take} from 'rxjs/operators';
  import { TestScheduler } from 'rxjs/testing';
  
  describe('CounterSessionProvider', () =&gt; {
    let counterSessionProvider: CounterSessionProvider;
    let scheduler: TestScheduler;
  
    beforeEach(() =&gt; {
      counterSessionProvider = new CounterSessionProvider();
    });
  
    /*beforeEach(() =&gt; {
      scheduler = new TestScheduler((actual, expected) =&gt; {
        expect(actual).toEqual(expected);
      });
    });*/
  
    /*it('should throw error when initInterval interval', fakeAsync(() =&gt; {
      scheduler.run(helpers =&gt; {
        spyOn(counterSessionProvider, 'initInterval').and.returnValue(throwError('ERROR'));
        const actions$ = helpers.hot('--a', { a: 1000 });
        const completion$ = helpers.cold('--c', { c: 1000 });
        expect(counterSessionProvider.initInterval).toBe(completion$);
      });
    }));*/
  
    it('should initialize currentSubscription from interval', fakeAsync(() =&gt; {
      let currentVal = null;
      const numberObservable = counterSessionProvider.initInterval();
      counterSessionProvider.currentSubscription = numberObservable.subscribe((value: number) =&gt; {
        currentVal = value;
      });
      tick(1000);
      expect(currentVal).toEqual(19);
      counterSessionProvider.currentSubscription.unsubscribe();
    }));
  
    it('should emit a String COMPLETE when start() is called', () =&gt; {
      spyOn(counterSessionProvider, 'start');
      counterSessionProvider.start();
      counterSessionProvider.counterStateEmitter.subscribe(value =&gt; {
        expect(value).toEqual('COMPLETE');
      });
      expect(counterSessionProvider.start).toHaveBeenCalled();
    });
  
    it('should call stop()', () =&gt; {
      spyOn(counterSessionProvider, 'stop');
      counterSessionProvider.currentSubscription = new Subscription();
      counterSessionProvider.stop();
      counterSessionProvider.counterStateEmitter.subscribe((value: string) =&gt; {
        expect(value).toEqual('ABORTED');
      });
      expect(counterSessionProvider.stop).toHaveBeenCalled();
    });
  
    it('should call method start and initilize currentSubscription', fakeAsync(() =&gt; {
      counterSessionProvider.start();
      tick(1000);
      counterSessionProvider.currentSubscription.unsubscribe();
      expect(counterSessionProvider.currentSubscription).not.toBeNull();
    }));
  
    it('should call restarCounterTime() and emit a TRUE', () =&gt; {
      counterSessionProvider.restarCounterTime();
      counterSessionProvider.substracCounter$.subscribe(value =&gt; expect(value).toBeTruthy());
    });
  
    it('should call method stop and emit ABORTED', fakeAsync(() =&gt; {
      counterSessionProvider.start();
      tick(1000);
      counterSessionProvider.stop();
    }));
  
    /*beforeEach(() =&gt; {
      counterSessionProvider = TestBed.get(CounterSessionProvider);
    });
  
    it('should be created', () =&gt; {
      expect(counterSessionProvider).toBeTruthy();
    });
  
    it('should return ABORTED when stop function is called', () =&gt; {
      counterSessionProvider.start();
      counterSessionProvider.counterState.subscribe((msg: string) =&gt; {
        expect(msg).toEqual('ABORTED');
      });
      counterSessionProvider.stop();
    });*/
  });
  </code></pre>
  <p>But I need to cover all the code of your coverage. In the following image I show the coverage that I need.</p>
  <p>Trying to apply the suggestions that Shashank Vivek gave me, it gives these results:</p>
  <p><a href="https://i.stack.imgur.com/l0VXO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l0VXO.png" alt="enter image description here" /></a></p>