Skip to content
Latchkey

Angular "NullInjectorError: No provider for X" in CI

Angular's dependency injector could not find a provider for a requested token. In tests, this fires when TestBed creates a component whose injected service was not added to the testing module providers.

What this error means

ng test fails with "NullInjectorError: R3InjectorError ... No provider for HttpClient!" (or another service) when a spec instantiates a component or service.

ng
NullInjectorError: R3InjectorError(DynamicTestModule)[UserService -> HttpClient ->
HttpClient]: NullInjectorError: No provider for HttpClient!

Common causes

A required provider is missing from TestBed

The component or service depends on a token (HttpClient, a custom service) that the testing module never provided.

A testing module not imported

For HttpClient, provideHttpClientTesting()/HttpClientTestingModule was not imported, so the token has no provider.

How to fix it

Provide the dependency in TestBed

  1. Read which token the error names.
  2. Add it to the TestBed providers or import the matching testing module.
  3. Re-run the spec.
user.service.spec.ts
TestBed.configureTestingModule({
  providers: [provideHttpClientTesting()],
});

Mock the service instead

Provide a stub for a custom service so the component under test gets a value without the real implementation.

spec.ts
providers: [{ provide: AuthService, useValue: authStub }]

How to prevent it

  • Provide or mock every injected dependency in TestBed setup.
  • Use the testing modules for HttpClient, Router, and similar.
  • Run unit tests in CI so missing providers surface on the PR.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →