mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
7a2efd442d
This commit handles two cases that were breaking applications when using the new migration: - tests using `HttpClientModule` in `TestBed.configureTestingModule` were broken as the import was removed, but the module is still present in the test configuration. It now properly adds `provideHttpClient(withInterceptorsFromDi())` and related imports to the test. - tests using `HttpClientTestingModule` were migrated to use `provideHttpClient(withInterceptorsFromDi())` but the necessary imports were not added. They are now added by the migration. PR Close #55640
Replace Http modules from @angular/common/http with provider functions
HttpClientModule, HttpClientXsrfModule, HttpClientJsonpModule are deprecated in favor of provideHttpClient and its options.
HttpClientTestingModule is deprecated in favor or provideHttpClientTesting()
This migration updates any @NgModule, @Component, @Directive that imports those modules.
Http Modules
Before
import { HttpClientModule, HttpClientJsonpModule, HttpClientXsrfModule } from '@angular/common/http';
@NgModule({
imports: [CommonModule, HttpClientModule, HttpClientJsonpModule, HttpClientXsrfModule]
})
export class AppModule {}
After
import { provideHttpClient, withJsonpSupport, withXsrfConfiguration } from '@angular/common/http';
@NgModule({
imports: [CommonModule],
providers: [provideHttpClient(withJsonpSupport(), withXsrfConfiguration())]
})
export class AppModule {}
Testing
Before
import { HttpClientTestingModule } from '@angular/common/http/testing';
describe('some test', () => {
it('...', () => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule]
})
})
})
After
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
describe('some test', () => {
it('...', () => {
TestBed.configureTestingModule({
providers: [provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting()]
})
})
})
Before
import { HttpClientTesting } from '@angular/common/http';
describe('some test', () => {
it('...', () => {
TestBed.configureTestingModule({
imports: [HttpClientTesting],
})
})
});
After
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
describe('some test', () => {
it('...', () => {
TestBed.configureTestingModule({
providers: [provideHttpClient(withInterceptorsFromDi())]
})
})
})