'Angular Unit Test for Http Error Interceptor not working

I have following http error interceptor:

@Injectable()
export class HttpErrorInterceptor implements HttpInterceptor {
  constructor(private util: UtilService,
    private matomoService: MatomoService) {}
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(request)
      .pipe(
        catchError((error: HttpErrorResponse) => {
          let data: any = {};
          if (error.error instanceof ErrorEvent) {
            // Client side error
            data = 'error'
            this.util.alerts$.next(data);
            this.matomoService.eventTracking('error', 'client_error', data.description);
          } else {
            // Server side error
            data = 'error';
            this.util.alerts$.next(data);
            this.matomoService.eventTracking('error', 'api_error', data.description);
          }
          console.log(error);
          return throwError(error.message);
        })
      );
  }
}

My unit test is as follows:

describe('HttpInterceptorService', () => {
  let injector: TestBed;
  let httpMock: HttpTestingController;
  let matomoService: MatomoService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [HttpErrorInterceptor,
      UtilService,
      MatomoService,
      SomeThemeService,
      {
        provide: HTTP_INTERCEPTORS,
        useClass: HttpErrorInterceptor,
        multi: true
      }
    ],
      imports: [RouterTestingModule,
      HttpClientTestingModule]
    });

    injector = getTestBed();
    matomoService = injector.get(MatomoService);
    httpMock = injector.get(HttpTestingController);
  });

  fit('should call matomo tracking for errors', () => {
    const ldapApiService = injector.get(LdapApiService);
    const matomoSpy = spyOn(matomoService, 'eventTracking');
    ldapApiService.getLDAPUsers('https://some-dummy-url').subscribe(res => {
      expect(res).toBeTruthy();
    });
    const httpReq = httpMock.expectOne('https://some-dummy-url');
    expect(matomoSpy).toHaveBeenCalledTimes(1);
  });

});

This fails with error Expected spy eventTracking to have been called once. It was called 0 times.

I am taking help from https://medium.com/@dev.s4522/how-to-write-unit-test-cases-for-angular-http-interceptor-7595cb3a8843 for writing this unit test. Can you please help why matomoSpy is not been called or how we can test this interceptor.



Solution 1:[1]

You must flush the request like this:

httpReq.flush();

Also, your expect must be in the subscribe. To make sure that the code in the subscribe is called, you must use done like this:

  fit('should call matomo tracking for errors', (done) => {
    const ldapApiService = injector.get(LdapApiService);
    const matomoSpy = spyOn(matomoService, 'eventTracking');
    ldapApiService.getLDAPUsers('https://some-dummy-url').subscribe(res => {
      expect(res).toBeTruthy();
      expect(matomoSpy).toHaveBeenCalledTimes(1);
      done();
    });
    const httpReq = httpMock.expectOne('https://some-dummy-url');
    httpReq.flush();
   });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JFPicard