Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

HttpClientTestingModule is deprecated, how to replace it?

After upgrading my application to Angular 18.0.4, my test classes say:

'HttpClientTestingModule' is deprecated. Add provideHttpClientTesting() to your providers instead.

Therefore I adapted my code as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        await TestBed.configureTestingModule(
            {
                imports: [
                    AssetDetailsComponent,
                ],
                providers: [
                    // replacement for HttpClientTestingModule:
                    provideHttpClientTesting() 
                ]
            })
            .compileComponents();

However, when I run the tests, I get the following error:

NullInjectorError: R3InjectorError(Standalone[AssetDetailsComponent])[InventoryActionService -> InventoryActionService -> _HttpClient -> _HttpClient]:
      NullInjectorError: No provider for _HttpClient!

If I use provideHttpClient() instead of provideHttpClientTesting() it works, yet I doubt that this is best practice. What is the correct solution to this issue?

>Solution :

Also add provideHttpClient() before provideHttpClientTesting()

providers: [
   provideHttpClient(),
   provideHttpClientTesting() 
]

As mentioned in the docs.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading