Mocked service throws NG0201: No provider found for `_HttpClient` error

Discussão em 'Angular' iniciado por mat.hudak, Agosto 27, 2025.

  1. mat.hudak

    mat.hudak Guest

    I'm writing unit tests for NX libraries and one service is driving me crazy. It injects generated services and calls their exposed methods.

    I'm using Jest + Spectator combination and I'm getting this error:


    ɵNotFound: NG0201: No provider found for _HttpClient. Source: Standalone[AppSearch]. Path: SearchService -> StructuredSearchService -> _HttpClient. Find more at https://angular.dev/errors/NG0201
    ​

    My Unit test:

    import { createServiceFactory, mockProvider, SpectatorService } from '@ngneat/spectator/jest';

    describe('SearchSearch', () => {
    let spectator: SpectatorService<SearchService>;
    const createService = createServiceFactory({
    service: SearchService,
    providers: [
    provideHttpClient(), // I believe those providers are in fact unnecessary
    provideHttpClientTesting(), // The service does not use Http directly
    mockProvider(SimpleSearchService, {
    simpleSearch: () => of({ items: [] }),
    }),
    mockProvider(StructuredSearchService, {
    structuredSearch: () => of({ items: [] }),
    }),
    // Ver. 2 - I tried this approach too
    // {
    // provide: SimpleSearchService,
    // useValue: {
    // simpleSearch: jest.fn().mockReturnValue(of({ items: [] })),
    // },
    // },
    // {
    // provide: StructuredSearchService,
    // useValue: {
    // structuredSearch: jest.fn().mockReturnValue(of({ items: [] })),
    // },
    // },
    // Ver. 3 - Spectator's Mocks array does not help either
    // mocks: [SimpleSearchService, StructuredSearchService],
    ],
    });


    It looks like Jest is ignoring my attempts at mocking OpenAPI generated services and fails on Http, which I don't really need. What did I forget of overlook? Versions:

    "@ngneat/spectator": "^21.0.0",
    "jest": "^29.0.0",
    "@angular/*": "^20.1.6",

    Continue reading...

Compartilhe esta Página