Search code examples
angularngrxtestbed

NullInjectorError of feature store when testing component


I try to fix the test of LoginComponent. The error came when the feature store (notification) component was moved to a lazy-loaded module from SharedModule. I have no idea what can be wrong here. I tested many configurations of the testing component with ngrx. Also, moving back NotificationModule (with feature store) doesn't fix the bug due to the strong-refactor that was made.

describe('LoginComponent', () => {
  let component: LoginComponent;
  let fixture: ComponentFixture<LoginComponent>;
  let store: MockStore<State>;


  beforeAll(() => {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
  });

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        StoreModule.forRoot(),
        HttpClientTestingModule,
        TranslateModule.forRoot(),
        ReactiveFormsModule,
        SharedModule,
      ],
      declarations: [LoginComponent],
      providers: [
        provideMockStore(),
      ]
    })
      .compileComponents();

    fixture = TestBed.createComponent(LoginComponent);
    component = fixture.componentInstance;
    store = TestBed.get(MockStore);

    spyOn(store, 'dispatch');
    fixture.detectChanges();
  }));

LoginModule which is imported in AppModule.

@NgModule({
  imports: [
    CommonModule,
    HttpClientModule,
    ReactiveFormsModule,
    SharedModule,
    TranslateModule,
  ],
  declarations: [
    LoginComponent,
  ],
})
export class LoginModule {}

Error:

Failed: StaticInjectorError(DynamicTestModule)[NotificationEffects -> Actions]: 
      StaticInjectorError(Platform: core)[NotificationEffects -> Actions]: 
        NullInjectorError: No provider for Actions!
    error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'NotificationEffects', Function ] })
        at <Jasmine>
        at NullInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:855:1)
        at resolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17514:1)
        at tryResolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17440:1)
        at StaticInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17266:1)
        at resolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17514:1)
        at tryResolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17440:1)
        at StaticInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17266:1)
        at resolveNgModuleDep (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:30393:1)
        at _createClass (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:30470:1)
        at _createProviderInstance (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:30426:1)
    Error: Expected undefined to be truthy.
        at <Jasmine>
        at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/login/login.component.ngrx.spec.ts:67:23)
        at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
        at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308:1)

Solution

  • The error says that NotificationEffects requires an injectable Actions and the module does not know how to provide it. Looking into the provided code extraction it is impossible to know what is Actions and how should it be provided. You can start figuring it out by looking into NotificationEffects source code.