Testing: Angular 22 practice questions
Testing is 10% of the free Angular 22 mock (5 of 50 questions). Pick an answer to see right away whether it is correct, with the explanation and the documentation section it comes from.
NG.X · Angular 22 practice question
After registering provideHttpClientTesting() in TestBed, which token should a test inject to expect and answer outgoing HTTP requests?
const httpTesting = TestBed.inject(HttpTestingController);
Check answer and explanation
Correct answer: HttpTestingController
Explanation
Injecting HttpTestingController via TestBed.inject(HttpTestingController) gives the test access to the controller used to match and respond to pending HTTP requests.
Common trap
This is tempting because older NgModule-based Angular tests imported HttpClientTestingModule, but it is a module rather than the injectable controller. This is tempting because HttpHandlerFn is used in functional interceptors, but it is not the testing controller token.
Source: Angular docs: Test requests — Expecting and answering requests
Checked against the cited text on 2026-11-25.
NG.X · Angular 22 practice question
A developer already uses a stub to control what a dependency returns, but now needs to verify that the service under test invoked that dependency with specific arguments. Which test double should they use?
Check answer and explanation
Correct answer: A spy, which records how a function is called so assertions can verify the arguments.
Explanation
While a stub controls return values, a spy tracks how a function is called so the test can assert on interactions and verify the arguments passed to a dependency.
Common trap
This is tempting because ComponentFixture is central to component testing, but fixtures are for components and DOM elements, not tracking service method calls. This is tempting because stubs replace dependencies in tests, but a plain stub only supplies predictable return values and does not track call arguments.
Source: Angular docs: Testing services — Verifying interactions with spies
Checked against the cited text on 2026-11-25.
NG.X · Angular 22 practice question
Before running Vitest unit tests in a real browser for an Angular project, what setup step is required first?
Check answer and explanation
Correct answer: Install a browser provider.
Explanation
To execute Vitest tests in a real browser, you must first install a browser provider for Vitest's browser mode.
Common trap
This is tempting because Node.js environments use DOM shims, but running in a real browser eliminates the need for a custom DOM shim rather than requiring one. This is tempting because browser mode switches away from the Node.js test environment, but you do not disable Node.js in package.json.
Source: Angular docs: Unit testing — Running tests in a browser
Checked against the cited text on 2026-11-25.
NG.X · Angular 22 practice question
Given a ComponentFixture in an Angular component test, which property gives direct access to the component's root DOM element?
const bannerElement: HTMLElement = fixture.nativeElement;
Check answer and explanation
Correct answer: fixture.nativeElement
Explanation
The fixture exposes the component's host DOM element directly through fixture.nativeElement.
Common trap
This is tempting because componentInstance is a real ComponentFixture property, but it returns the TypeScript component class instance rather than the DOM element. This is tempting because ElementRef is used inside components to reference host elements, but ComponentFixture exposes nativeElement and debugElement instead.
Source: Angular docs: Basics of testing components — `DebugElement`
Checked against the cited text on 2026-11-25.
NG.X · Angular 22 practice question
In this functional HTTP interceptor, how does authInterceptor obtain an instance of AuthService?
export function authInterceptor(
request: HttpRequest<unknown>,
next: HttpHandlerFn,
): Observable<HttpEvent<unknown>> {
const authService = inject(AuthService);
const clonedRequest = request.clone({
headers: request.headers.append('X-Authentication-Token', authService.getAuthToken()),
});
return next(clonedRequest);
}
Check answer and explanation
Correct answer: By calling inject(AuthService) inside the interceptor function.
Explanation
Functional interceptors run in an injection context, so authInterceptor retrieves AuthService by calling inject(AuthService).
Common trap
This is tempting because dependencies are sometimes passed as function parameters, but HttpInterceptorFn only receives request and next. This is tempting because tests use TestBed.inject(), but production interceptor code must use inject() rather than TestBed.
Source: Angular docs: Test requests — Testing an Interceptor
Checked against the cited text on 2026-11-25.
NG.X · Angular 22 practice question
A team has finished setting up unit testing for their Angular application and wants to enforce minimum test coverage thresholds in CI. Which testing topic addresses measuring coverage and configuring required thresholds?
Check answer and explanation
Correct answer: Code coverage
Explanation
Code coverage tracks how much of the application is exercised by tests and explains how to configure required coverage amounts.
Common trap
This is tempting because CI pipelines often run browser tests, but browser mode controls the execution environment rather than coverage thresholds. This is tempting because DOM tests increase coverage of templates, but component DOM testing does not define coverage reporting or threshold rules.
Source: Angular docs: Unit testing — More information on testing
Checked against the cited text on 2026-11-25.
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$);
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.
NG.X · Angular 22 practice question
An interceptor needs to attach an X-Authentication-Token header to every outgoing HTTP request before forwarding it. How should it modify the request?
const clonedRequest = request.clone({
headers: request.headers.append('X-Authentication-Token', authService.getAuthToken()),
});
return next(clonedRequest);
Check answer and explanation
Correct answer: Call request.clone() with request.headers.append(...) and pass the cloned request to next().
Explanation
HttpRequest and HttpHeaders are immutable, so the interceptor clones the request with the appended header and forwards the cloned request to next().
Common trap
This is tempting because mutating objects is common in JavaScript, but HttpRequest and HttpHeaders are immutable so in-place mutation does not attach the header. This is tempting because interceptors return Observable<HttpEvent<unknown>>, but skipping next() would prevent the request from continuing down the handler chain.
Source: Angular docs: Test requests — Testing an Interceptor
Checked against the cited text on 2026-11-25.
NG.X · Angular 22 practice question
A team normally runs unit tests in the default Node.js environment for speed. In which situation should they run their tests in a real browser instead?
Check answer and explanation
Correct answer: When tests rely on browser-specific APIs such as rendering, or when debugging in a browser.
Explanation
Although Node.js is faster for most unit tests, running in a real browser is useful when tests depend on browser-specific APIs like rendering or when you need browser debugging.
Common trap
This is tempting because TestBed is an Angular testing utility, but service injection with TestBed.inject() runs fine in the default Node.js environment. This is tempting because HTTP requests normally happen in a browser, but provideHttpClientTesting() intercepts requests in memory and does not require a real browser.
Source: Angular docs: Unit testing — Running tests in a browser
Checked against the cited text on 2026-11-25.
NG.X · Angular 22 practice question
When testing a Lightswitch component that exposes a clicked() method and a message property, which question requires component DOM testing rather than a class-only unit test?
Check answer and explanation
Correct answer: Whether clicked() is bound in the template so a user can trigger it and see message displayed.
Explanation
Verifying that clicked() is bound to a template element for the user to invoke and that message is actually rendered requires testing the component together with its DOM.
Common trap
This is tempting because toggling state is part of the component's behavior, but checking an internal field after calling a method directly can be done in a class-only test. This is tempting because strict compilation catches many template errors, but compilation alone does not verify runtime DOM behavior or user interaction.
Source: Angular docs: Basics of testing components — Component DOM testing
Checked against the cited text on 2026-11-25.
NG.X · Angular 22 practice question
A developer wants to unit test OrderTotal without depending on the real TaxCalculator implementation. What is the recommended way to isolate OrderTotal?
Check answer and explanation
Correct answer: Provide a stub for TaxCalculator in the TestBed configuration so it returns predictable values.
Explanation
Providing a stub for TaxCalculator in the TestBed configuration replaces the real dependency with one that returns predictable values, making OrderTotal's behavior easy to verify.
Common trap
This is tempting because browser mode changes the test environment, but it does not replace or isolate a service dependency. This is tempting because HttpTestingController isolates HTTP calls, but it only intercepts HttpClient requests, not direct method calls on another service.
Source: Angular docs: Testing services — Replacing a dependency with a stub
Checked against the cited text on 2026-11-25.
NG.X · Angular 22 practice question
A developer writes unit tests that instantiate a component class directly with new MyComponent() and check its methods. Why are these class-only tests insufficient on their own?
Check answer and explanation
Correct answer: They cannot verify DOM rendering, user input handling, or integration with parent and child components.
Explanation
Testing only the TypeScript class misses everything that happens in the template and DOM, including whether the component renders properly, handles user input and gestures, or integrates with parent and child components.
Common trap
This is tempting because TestBed is the standard way to test components, but instantiating a component class directly in plain TypeScript is possible; it just leaves the template and DOM untested. This is tempting because signals are tied to Angular's reactivity model, but reading and updating signals works in plain class tests without a DOM.
Source: Angular docs: Basics of testing components — Component DOM testing
Checked against the cited text on 2026-11-25.