From ffae7df6e876eaecffd8550efce460139a26f3d5 Mon Sep 17 00:00:00 2001 From: Matthieu Riegler Date: Wed, 4 Dec 2024 17:56:21 +0100 Subject: [PATCH] refactor(http): Don't log fetch warning in tests. (#59049) Prior to this commit, we were logging the `NOT_USING_FETCH_BACKEND_IN_SSR` error when `provideHttpTestingClient` and `PLATFORM_ID` were provided. fixes #59028 PR Close #59049 --- packages/common/http/src/interceptor.ts | 9 ++++++++- packages/common/http/testing/src/backend.ts | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/common/http/src/interceptor.ts b/packages/common/http/src/interceptor.ts index 192765d69aa..db3bec95fcf 100644 --- a/packages/common/http/src/interceptor.ts +++ b/packages/common/http/src/interceptor.ts @@ -276,7 +276,14 @@ export class HttpInterceptorHandler extends HttpHandler { // a warning otherwise. if ((typeof ngDevMode === 'undefined' || ngDevMode) && !fetchBackendWarningDisplayed) { const isServer = isPlatformServer(injector.get(PLATFORM_ID)); - if (isServer && !(this.backend instanceof FetchBackend)) { + + // This flag is necessary because provideHttpClientTesting() overrides the backend + // even if `withFetch()` is used within the test. When the testing HTTP backend is provided, + // no HTTP calls are actually performed during the test, so producing a warning would be + // misleading. + const isTestingBackend = (this.backend as any).isTestingBackend; + + if (isServer && !(this.backend instanceof FetchBackend) && !isTestingBackend) { fetchBackendWarningDisplayed = true; injector .get(Console) diff --git a/packages/common/http/testing/src/backend.ts b/packages/common/http/testing/src/backend.ts index 7cb291d7175..809f359501c 100644 --- a/packages/common/http/testing/src/backend.ts +++ b/packages/common/http/testing/src/backend.ts @@ -31,6 +31,11 @@ export class HttpClientTestingBackend implements HttpBackend, HttpTestingControl */ private open: TestRequest[] = []; + /** + * Used when checking if we need to throw the NOT_USING_FETCH_BACKEND_IN_SSR error + */ + private isTestingBackend = true; + /** * Handle an incoming request by queueing it in the list of open requests. */