NG.X · Angular 22 practice question

In this HttpClient test snippet, why does the test pass config$ to firstValueFrom()?

const service = TestBed.inject(ConfigService);
const config$ = service.getConfig<Config>();
const configPromise = firstValueFrom(config$);
Choose one answer
Check answer and explanation

Correct answer: It subscribes to the Observable to trigger the HTTP request and returns a Promise of the response.

Explanation

Because HttpClient Observables are cold, passing config$ to firstValueFrom() subscribes to the Observable—which actually initiates the HTTP request—and produces a Promise that resolves with the response.

Common trap

This is tempting because tests often await promises after flushing, but firstValueFrom() only subscribes and awaits; you still flush the request through HttpTestingController. This is tempting because async providers exist in some frameworks, but firstValueFrom() is an RxJS utility operating on the Observable, not on TestBed.

Source: Angular docs: Test requests — Expecting and answering requests
Checked against the cited text on 2026-11-25.

Report an error in this question