Sub topic 14
Overriding providers in tests
This chapter assumes you know how to write a basic Angular unit test with TestBed, how standalone components declare providers, and how element injectors and environment injectors differ. The goal is to replace a scoped service with a fake in a test.
Why module-level providers are not enough
TestBed.configureTestingModule({ providers: [...] }) registers providers in the test's root environment injector. A component that lists a service in its own providers array creates an element injector entry. Element injector entries are found before environment injector entries, so the component's own provider wins.
The result is a common surprise. You add a fake to the testing module, and the component still gets the real service.
Replacing component providers with overrideComponent
TestBed.overrideComponent changes the component's metadata before it is created. Use set to replace the providers array.
The component under test registers CounterService in its own providers:
import { Component, inject } from '@angular/core';
import { CounterService } from './counter.service';
@Component({
selector: 'app-counter',
standalone: true,
providers: [CounterService],
template: `<button (click)="counter.increment()">{{ counter.count() }}</button>`,
})
export class CounterComponent {
protected readonly counter = inject(CounterService);
}The test swaps that entry for a fake:
import { TestBed } from '@angular/core/testing';
import { CounterComponent } from './counter.component';
import { CounterService } from './counter.service';
class FakeCounterService {
readonly count = () => 42;
increment(): void {}
}
it('renders the count from a component-scoped service', () => {
TestBed.configureTestingModule({ imports: [CounterComponent] });
TestBed.overrideComponent(CounterComponent, {
set: { providers: [{ provide: CounterService, useClass: FakeCounterService }] },
});
const fixture = TestBed.createComponent(CounterComponent);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toContain('42');
});Call overrideComponent before TestBed.createComponent. After the first component is created, TestBed no longer accepts overrides.
Choosing where to override
The right place depends on where the real provider is declared.
| Where the service is provided | How to replace it in a test |
|---|---|
| `providedIn: 'root'` | Add `{ provide: X, useClass: Fake }` to `configureTestingModule` providers |
| Component `providers` | `TestBed.overrideComponent` with `set` (or `add`) |
| Route `providers` | Provide the fake in the testing module, or configure the test router with the fake in the route's `providers` |
For a root service, the testing module is the right tool, because both sit in the same environment injector:
TestBed.configureTestingModule({
providers: [{ provide: CounterService, useClass: FakeCounterService }],
});Checking which instance you got
Scoped services produce one instance per component. A test can confirm this by reading the injector from the fixture.
const injected = fixture.debugElement.injector.get(CounterService);
expect(injected).toBeInstanceOf(FakeCounterService);debugElement.injector is the element injector of that component. If this returns the real class, your override did not apply to the injector you think it did.
Common mistakes
- Overriding after
createComponenthas run. - Putting the fake in the testing module when the component declares its own
providers. - Using
addwhen you meantset. Withadd, the original provider stays in the array, so check which entry wins. - Forgetting that each component instance gets its own fake, so state is not shared between two instances of the component.
Exercise
A component lists ThemeService in its providers. Your test adds { provide: ThemeService, useClass: FakeThemeService } to TestBed.configureTestingModule and the test still sees the real service. Why, and how do you fix it?
Show answer
The component's own element injector provides ThemeService, and it is checked before the root environment injector that holds the testing module providers. Call TestBed.overrideComponent(Component, { set: { providers: [{ provide: ThemeService, useClass: FakeThemeService }] } }) before TestBed.createComponent.