Sub topic 13
Testing a feature in isolation
This chapter assumes you can write Angular unit tests with TestBed, know the Jasmine or Jest basics, and have read the earlier chapters on route-scoped providers and feature state. It uses the orders feature from those chapters.
What "isolation" means here
A feature is isolated when its tests need nothing from other features. They use the feature's own files and fake only what crosses its boundary. Two boundaries matter:
- The network, which the store reaches through
HttpClient. - Other features, which are reached only through their public API barrels.
Because OrdersStore has no providedIn setting, no test gets it by accident. Each test lists it in providers, which keeps every test explicit about what it uses.
Test the store against fake HTTP
The store holds the feature's logic, so test it first. provideHttpClientTesting() replaces the real backend, and HttpTestingController lets you answer requests by hand.
import { TestBed } from '@angular/core/testing';
import { provideHttpClient } from '@angular/common/http';
import {
HttpTestingController,
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { OrdersStore } from './orders.store';
import { Order } from './order.model';
describe('OrdersStore', () => {
let store: OrdersStore;
let http: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [OrdersStore, provideHttpClient(), provideHttpClientTesting()],
});
store = TestBed.inject(OrdersStore);
http = TestBed.inject(HttpTestingController);
});
afterEach(() => http.verify());
it('stores loaded orders and updates the count', () => {
const orders = [{ id: '1' }, { id: '2' }] as unknown as Order[];
store.load();
http.expectOne('/api/orders').flush(orders);
expect(store.orders()).toEqual(orders);
expect(store.count()).toBe(2);
});
});http.verify() fails the test if a request was made that the test never answered. That catches stray calls.
Test the component against a fake store
The component test should not care how the store fetches data. Provide a plain object with the same shape, built from signals. This sketch assumes OrderListComponent reads store.orders() and calls store.load() when it starts.
import { signal } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { OrderListComponent } from './order-list.component';
import { OrdersStore } from '../data-access/orders.store';
import { Order } from '../data-access/order.model';
describe('OrderListComponent', () => {
let fixture: ComponentFixture<OrderListComponent>;
const load = jasmine.createSpy('load');
beforeEach(() => {
const orders = signal([{ id: '1' }] as unknown as Order[]);
TestBed.configureTestingModule({
imports: [OrderListComponent],
providers: [
{
provide: OrdersStore,
useValue: { orders: orders.asReadonly(), count: signal(1), load },
},
],
});
fixture = TestBed.createComponent(OrderListComponent);
});
it('asks the store to load orders', () => {
fixture.detectChanges();
expect(load).toHaveBeenCalled();
});
});The component is standalone, so it goes in imports, not declarations. The fake replaces the store without any HTTP setup.
Common errors
NullInjectorError: No provider for OrdersStore. The route supplies the store in production, but a component test has no route. Provide it in the test.- Importing another feature's internals. If a test reaches into
customers/data-access/..., the feature is not isolated. Import from the barrel or an alias such as@app/orders, or fake the dependency. - Forgetting
provideHttpClient().provideHttpClientTesting()alone does not registerHttpClient.
Exercise
A component test for OrderDetailComponent fails with "No provider for OrdersStore", even though the app works in the browser. Why does it fail, and what are two ways to fix it?
Show answer
In the app, the store is supplied by the providers entry on the orders route. A TestBed test does not run that route, so nothing provides the store. Fix it by adding OrdersStore to the test's providers (with HTTP testing providers), or by providing a fake object with { provide: OrdersStore, useValue: fake }. The fake keeps the test independent of HTTP.