Skip to content

Commit ab5ede9

Browse files
committed
docs: update HTTP testing setup guidance
Update testing documentation clarify HttpClient testing providers Fixed angular#68792
1 parent 16fe27b commit ab5ede9

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

adev/src/content/guide/http/testing.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ Finally, tests can verify that the app made no unexpected requests.
88

99
## Setup for testing
1010

11-
To begin testing usage of `HttpClient`, configure `TestBed` and include `provideHttpClient()` and `provideHttpClientTesting()` in your test's setup. This configures `HttpClient` to use a test backend instead of the real network. It also provides `HttpTestingController`, which you'll use to interact with the test backend, set expectations about which requests have been made, and flush responses to those requests. `HttpTestingController` can be injected from `TestBed` once configured.
12-
13-
IMPORTANT: Keep in mind to provide `provideHttpClient()` **before** `provideHttpClientTesting()`, as `provideHttpClientTesting()` will overwrite parts of `provideHttpClient()`. Doing it the other way around can potentially break your tests.
11+
To begin testing usage of `HttpClient`, configure `TestBed` and include `provideHttpClientTesting()` in your test's setup. `HttpClient` is provided by Angular's test environment, and `provideHttpClientTesting()` configures it to use a test backend instead of the real network. It also provides `HttpTestingController`, which you'll use to interact with the test backend, set expectations about which requests have been made, and flush responses to those requests. `HttpTestingController` can be injected from `TestBed` once configured.
1412

1513
```ts
1614
TestBed.configureTestingModule({
1715
providers: [
1816
// ... other test providers
19-
provideHttpClient(),
2017
provideHttpClientTesting(),
2118
],
2219
});
@@ -26,13 +23,25 @@ const httpTesting = TestBed.inject(HttpTestingController);
2623

2724
Now when your tests make requests, they will hit the testing backend instead of the normal one. You can use `httpTesting` to make assertions about those requests.
2825

26+
### Configuring `HttpClient` in tests
27+
28+
If a test needs to configure `HttpClient` features, such as interceptors, include `provideHttpClient(...)` before `provideHttpClientTesting()`. `provideHttpClientTesting()` will overwrite parts of `provideHttpClient()`, so doing it the other way around can potentially break your tests.
29+
30+
IMPORTANT: Keep in mind to provide `provideHttpClient()` **before** `provideHttpClientTesting()`, as `provideHttpClientTesting()` will overwrite parts of `provideHttpClient()`. Doing it the other way around can potentially break your tests.
31+
32+
```ts
33+
TestBed.configureTestingModule({
34+
providers: [provideHttpClient(withInterceptors([authInterceptor])), provideHttpClientTesting()],
35+
});
36+
```
37+
2938
## Expecting and answering requests
3039

3140
For example, you can write a test that expects a GET request to occur and provides a mock response:
3241

3342
```ts
3443
TestBed.configureTestingModule({
35-
providers: [ConfigService, provideHttpClient(), provideHttpClientTesting()],
44+
providers: [ConfigService, provideHttpClientTesting()],
3645
});
3746

3847
const httpTesting = TestBed.inject(HttpTestingController);

0 commit comments

Comments
 (0)