Files
cexbrayat 65b7cb2826 fix(migrations): migrate HttpClientTestingModule in test modules (#55803)
The migration was breaking tests with test modules that imported `HttpClientTestingModule`,
as it removed the JS imports without migrating the module imports.

The migration now handles the case where `HttpClientTestingModule` is used in test modules,
by replacing the module import with the `provideHttpClient` and `provideHttpClientTesting` providers.

Before:
```ts
import { HttpClientTestingModule } from '@angular/common/http/testing';

@NgModule({
  declarations: [AppComponent],
  imports: [HttpClientTestingModule],
})
export class TestModule {}
```

After:
```ts
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';

@NgModule({
  declarations: [AppComponent],
  imports: [],
  providers: [provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting()]
})
export class TestModule {}
```

PR Close #55803
2024-05-15 08:45:31 -07:00
..

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())]
      })
    })
})