Your NG0201: No provider found for _HttpClient error is a classic dependency chain issue! 🔍
The problem: Your StructuredSearchService
needs HttpClient
, but Spectator's mockProvider()
doesn't handle transitive dependencies automatically.
Here's how @halverscheid-fiae.de/angular-testing-factory solves this:
// Spectator setup hell with dependency chain issues
const createService = createServiceFactory({
service: SearchService,
providers: [
provideHttpClient(), // ← Unnecessary complexity
provideHttpClientTesting(), // ← More boilerplate
mockProvider(SimpleSearchService, {
simpleSearch: () => of({ items: [] }),
}),
mockProvider(StructuredSearchService, {
structuredSearch: () => of({ items: [] }),
}),
// Still fails because HttpClient dependency chain is broken! 💥
],
});
import { createServiceFactory } from '@ngneat/spectator/jest';
import { provideHttpClientMock, createCustomServiceProviderMock } from '@halverscheid-fiae.de/angular-testing-factory';
const createService = createServiceFactory({
service: SearchService,
providers: [
// 🎯 One-line HttpClient mock that handles ALL dependency chains
provideHttpClientMock(),
// 🛡️ Type-safe service mocks with compile-time validation
createCustomServiceProviderMock(SimpleSearchService, {
simpleSearch: jest.fn(() => of({ items: [] }))
} satisfies jest.Mocked<Partial<SimpleSearchService>>),
createCustomServiceProviderMock(StructuredSearchService, {
structuredSearch: jest.fn(() => of({ items: [] }))
} satisfies jest.Mocked<Partial<StructuredSearchService>>),
],
});
provideHttpClientMock()
provides HttpClient for ALL dependent services// For quick setup, mock everything at once:
const createService = createServiceFactory({
service: SearchService,
providers: provideAngularCoreMocks({
httpClient: true, // ← Handles all HttpClient needs
}),
});
// Then override specific methods in your tests:
beforeEach(() => {
const httpMock = spectator.inject(HttpClient);
jest.spyOn(httpMock, 'get').mockReturnValue(of({ items: [] }));
});
npm install --save-dev @halverscheid-fiae.de/angular-testing-factory
Result: No more NG0201 errors, clean dependency resolution, type-safe mocks! 🎉
// If you still have provider issues, use the debug helper:
import { TEST_DATA } from '@halverscheid-fiae.de/angular-testing-factory';
beforeEach(() => {
console.log('Available providers:', spectator.inject(Injector));
// Helps identify missing dependencies
});
P.S. - Your NX + generated services struggle is exactly why I added the createCustomServiceProviderMock
function! 😅
Hope this saves you from the NG0201 nightmare! 🎯
// TypeScript catches mock inconsistencies at compile-time!
const provideMyServiceMock = createServiceProviderFactory(MyService, {
registerUser: jest.fn(() => of(mockResponse)),
// ↑ TypeScript validates this matches your real service
} satisfies jest.Mocked<Partial<MyService>>);
npm install --save-dev @halverscheid-fiae.de/angular-testing-factory
// Replace your entire beforeEach setup with:
import { provideHttpClientMock } from '@halverscheid-fiae.de/angular-testing-factory';
TestBed.configureTestingModule({
providers: [provideHttpClientMock()]
});
P.S. - Your 6 hours of frustration inspired this exact use case in the library! 😅
provideRouterMock()
provideActivatedRouteMock({ params: of({ id: '1' }) })
provideFormBuilderMock()
provideAngularCoreMocks({ httpClient: true, router: true })
npm install --save-dev @halverscheid-fiae.de/angular-testing-factory
The goal is simple: Write tests, not mock configuration. �