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.
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
- Read which token the error names.
- Add it to the TestBed
providersor import the matching testing module. - Re-run the spec.
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.
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.