shallow-render icon indicating copy to clipboard operation
shallow-render copied to clipboard

No provider for HttpClient!

Open PavlosMac opened this issue 4 years ago • 6 comments

Quick one here. Why do I need to provide the httpClient when using replaceModule with the testing module?

Error: NullInjectorError: No provider for HttpClient!

    const { inject, instance } = await shallow.replaceModule(HttpClient, HttpClientTestingModule).createService();

    const http = inject(HttpTestingController);

    const route = new ActivatedRouteSnapshot();
    instance.resolve(route, null);

    const mock = http.expectOne('http://localhost:8005/api/companies/');

    expect(mock.request.method).toBe('OPTIONS');
  });

PavlosMac avatar Dec 20 '20 16:12 PavlosMac

My best guess is that you need to dontMock the testing module too.

getsaf avatar Dec 22 '20 00:12 getsaf

If I swap things about, I can get rid of that issue, seemingly. However, now injecting the httpTestingController itself causes a problem. I feel close though. No provider for HttpTestingController!

  beforeEach(async () => {

 
    const {instance, inject} = await shallow.provide(HttpClient).replaceModule(HttpClientModule, HttpClientTestingModule).dontMock(HttpClientTestingModule)

    _instance = instance;
    _inject = inject;
    httpMock = inject(HttpTestingController)
  });

Any ideas?

PavlosMac avatar Dec 22 '20 12:12 PavlosMac

Hey, was just running through the issues for shallow-render and wanted to check in with you on this. I noticed that I was wrong about needing the dontMock here, replacement modules are never mocked. I suspect that the issue is that HttpClientModule is not imported in your service module.

Did you ever find a solution?

getsaf avatar Feb 04 '21 14:02 getsaf

Apologies for the late response. I did not find a solution and resorted to using the native TestBed configuration for the HttpTestingController.

PavlosMac avatar Mar 26 '21 14:03 PavlosMac

I decided to try reproducing this again and I'm still not able see this error. Here's a quick repo that contains a standard service that uses the HttpClientModule with tests in NG12. Is there anything that stands out as being different from your setup that could be causing my tests to pass and yours to get this error?

https://github.com/getsaf/shallow-render-ng12

getsaf avatar Jun 12 '21 15:06 getsaf

I was able to resolve this issue by just importing the HttpClientTestingModule when creating the instance of the Shallow().

eg., new Shallow(EventMenuComponent, EventMenuModule) .import(HttpClientTestingModule);

dreamweiver avatar Jun 05 '22 14:06 dreamweiver