shallow-render
shallow-render copied to clipboard
No provider for HttpClient!
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');
});
My best guess is that you need to dontMock
the testing module too.
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?
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?
Apologies for the late response. I did not find a solution and resorted to using the native TestBed configuration for the HttpTestingController
.
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
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);