Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();

}));

like image 576
tamilselvan s Avatar asked Dec 05 '19 08:12

tamilselvan s


2 Answers

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

like image 98
Anmol Narang Avatar answered Oct 23 '22 05:10

Anmol Narang


These approaches did not work for me. I had to provide my own value for the provider. This is what worked for me:

First, I declared my own "dummy" implementation:

const toastrService = {
    success: (message?: string, title?: string, override?: Partial<IndividualConfig>) => { },
    error: (message?: string, title?: string, override?: Partial<IndividualConfig>) => { }
  };

Then, I indicated the value to use in the providers section:

providers: [
        ...
        { provide: ToastrService, useValue: toastrService },
      ],
like image 20
Rene Enriquez Avatar answered Oct 23 '22 06:10

Rene Enriquez