I have an Angular application and when running unit tests with Karma, I get an error in saying that there is a missing provider for the Store in a component. The store is not directly injected into the component, but into a service that is injected into the component.
If I import the StoreModule into the component, the error disappears, but I shouldn't have to do that as the component does not require the store directly.
Error:
Error: StaticInjectorError(DynamicTestModule)[StoreRootModule -> Store]: StaticInjectorError(Platform: core)[StoreRootModule -> Store]: NullInjectorError: No provider for Store!
Component
export class TestComponent {
constructor(private testService: TestService) {}
}
Service
@Injectable({
providedIn: 'root'
})
export class TestService {
constructor(private store: Store<AppState>) {}
}
Component unit test
class MockTestService {
}
describe('TestComponent', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let resetMemorablePhraseService: MockTestService;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TestComponent],
imports: [
// StoreModule.forRoot({}) // adding this line gets rid of the error
],
providers: [{
provide: TestService, useClass: MockTestService
}]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges();
service = TestBed.get(TestService);
});
}