Skip to content

getting "NullInjectorError ..." error in unit testing #87

Open
@samhoseinkhani

Description

@samhoseinkhani

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions