Search code examples
angularunit-testingeslintngx-toastr

NullInjectorError: StaticInjectorError(DynamicTestModule)[ToastrService -> InjectionToken ToastConfig]: in tslint angular 8


When i run the unit testing in angular 8 project, i found an error in ngx-toastr

NullInjectorError: StaticInjectorError(DynamicTestModule)[ToastrService -> InjectionToken ToastConfig]:

And i imported the requierd modules in the spec.ts file, And also i declared forRoot() in app.module.ts

  beforeEach(async(() => {
TestBed.configureTestingModule({
  imports: [MatTabsModule,
    ReactiveFormsModule,
    MatTooltipModule,
    HttpClientTestingModule,
    RouterTestingModule,
    ToastrModule
  ],
  declarations: [CommunicationComponent],
  providers: [
    ToastrService,
  ]
})
  .compileComponents();

}));


Solution

  • import { ToastrModule } from 'ngx-toastr';

    beforeEach(async(() => {
        TestBed.configureTestingModule({
          imports: [ToastrModule.forRoot()],
    
        })
          .compileComponents();
      }));
    

    Add ToastrModule.forRoot() in imports as shown above and your error might be resolved