i18n-polyfill icon indicating copy to clipboard operation
i18n-polyfill copied to clipboard

getting "NullInjectorError ..." error in unit testing

Open samhoseinkhani opened this issue 4 years ago • 1 comments

i have an application which is a bit big , i needed " i18n-polyfill " to be in root of my application ... i did it as exact same way done in your example then i imported i18n seperatly into my component as a service and used it so well with no errors , then i came to write a test for my component . i added TRANSTLATION , TRANSTLATION_FORMAT and I18n in providers array of my testBed then it come with error that

NullInjectorError: StaticInjectorError(DynamicTestModule) [MyComponent -> I18n]:
StaticInjectorError(Platform: core)[MyComponent -> I18n]:
NullInjectorError: No provider for I18n !

this is my root app.module:

import {LOCALE_ID, NgModule, TRANSLATIONS, TRANSLATIONS_FORMAT} from "@angular/core";
import {I18n} from "@ngx-translate/i18n-polyfill";
@NgModule({
  declarations: [AppComponent],
  imports: [ChildModule],
  providers: [
    {provide: TRANSLATIONS_FORMAT, useValue: "xlf"},
    {provide: TRANSLATIONS, useValue: require("raw-loader!../i18n/messages.fr.xlf")},
    {provide: LOCALE_ID, useValue: "fr"},
    I18n
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

this is my child child.module:

@NgModule({
  declarations: [ChildComponent],
  imports: []
})
export class ChildModule {}

this is my child.component

import {Component} from "@angular/core";
import {I18n} from "@ngx-translate/i18n-polyfill";

@Component({
  selector: "app-child-component",
  templateUrl: "./app.child.component.html",
  styleUrls: ["./app.child.component.css"]
})
export class ChildComponent {
  title = "app";

  constructor(private i18n: I18n) {
    console.log(this.i18n(this.i18n("i should get translated dynamic")));
  }
}

**** note that my app by it self runs ok and there is no problem with translating "i should get translated dynamic" , the only problem is with test

samhoseinkhani avatar Dec 23 '19 07:12 samhoseinkhani

@ocombe do have any idea ?

samhoseinkhani avatar Dec 23 '19 07:12 samhoseinkhani