angularunit-testinggraphqlapolloapollo-angular

apollo-angular throws Error when using a mockError on Jasmine/Karma testing


I've tested errors on queries/mutations like these for years, but just now, I got one error I can't seem to figure it out.

When I call this test, Apollo is throwing an error for the errorMock I've made. Has anyone ever got this error?

It's funny because other tests that uses the same logic are working just fine, just this one

  it('should call errorHandler if getThirdPartyCompanies requests returns an error', fakeAsync(() => {
    const spyOnHandleError = spyOn(component['errorHandlerService'], 'handleError');

    component.getThirdPartyCompanies();
    const op = controller.expectOne(thirdPartyCouriersGraphqlModel);
    op.flush(
      {
        errors: [new GraphQLError('a')]
      }
    );
    controller.verify();
    tick(100);
    flush();

    expect(spyOnHandleError).toHaveBeenCalled();
  }));

image

Any tips on why this is happening?

Thanks in advance!


Solution

  • So....

    public getThirdPartyCompanies(): void {
          console.log('getThirdPartyCompanies');
          this.courierService.getThirdPartyCourierList(this.cityId)
                             .pipe(takeUntil(this.subscriptionDestroyer))
                             .subscribe((req) => {
            this.thirdPartyCourierList = req.data.thirdPartyCourierCompanies;
            this.loadingThirdies = false;
          }
          ), err =>{
            this.errorHandlerService.handleError(err);
            this.loadingThirdies = false;
          };
        }
    

    Here was the code I was using for the call. Notice that I was closing the observable before the error call.

    All I had to do was:

     }, err =>{
                this.errorHandlerService.handleError(err);
                this.loadingThirdies = false;
              };
            });
    

    Close the subscription the right way, and I got it working...

    For anyone having the same problem, here's the answer!