Search code examples
angularangular-httpclient

HttpClient testing issue: No provider for HttpClient with nested services


I have a problem upgrading to HttpClient from Http in Angular 4. I have a service which has a nested reference to HttpClient. My structure looks like this: My TokenHandlerService injects MyHttpService which injects Angulars HttpClient

Running the code snippets I found online looks like this:

let service: TokenHandlerService;
let httpMock: HttpTestingController;

beforeEach(() => {
    TestBed.configureTestingModule({
        imports: [
            HttpClientModule,
            HttpClientTestingModule
        ],
        providers: [
            TokenHandlerService,
            MyHttpService
        ]
    });
    httpMock = TestBed.get(HttpTestingController);
    service = TestBed.get(TokenHandlerService);
});

I want to test the TokenHandlerService, but running this I always end up in an Error: No provider for HttpClient!.

How can I pass the mocked HttpClient to deeper nested services?


Solution

  • Thanks to the support team at github I was able to fix this by adding:

    import 'core-js/es7/reflect';
    

    to my test.ts file.