Error: No provider for TranslateStore
Asked Answered
C

3

19

"Error: No provider for TranslateStore!" while UI Unit testing translation service in Angular 4. After I run ng test command I get the test cases failed error along with the above error message.

Consolidation answered 6/12, 2017 at 10:58 Comment(1)
that is not very much information you're providing to us, it would be easier to get some code of the tests that crash, so we can help you betterWomanize
N
33

Try this:

import {TranslateFakeLoader,TranslateLoader,TranslateModule,TranslateService } from '@ngx-translate/core';

TestBed.configureTestingModule({
      imports: [
        ...
        TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,
            useClass: TranslateFakeLoader
          }
        })
      ],
      ...
      providers: [
        TranslateService
      ]
Night answered 22/9, 2018 at 22:37 Comment(1)
Please check this out: npmjs.com/package/@ngx-translate/coreNight
T
5

Importing & adding TranslateStore to the providers in my lazy loaded module resolved issue for me.

import { TranslateModule,TranslateService,TranslateStore } from '@ngx-translate/core';

export function createTranslateLoader(http: HttpClient) {
  return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

    @NgModule({
      declarations: [...],
      imports: [
        TranslateModule.forChild(
            {
            loader: {
              provide: TranslateLoader,
              useFactory: (createTranslateLoader),
              deps: [HttpClient]
          }
        
              })],
              providers:[TranslateStore ]
    })

Check the issue link below for more information

https://github.com/ngx-translate/core/issues/883#issuecomment-502037966

Tonguing answered 1/12, 2020 at 14:11 Comment(2)
After that my translations do not workSemiporcelain
This seemed to be the solution for me (and my translations still work)Gandhiism
C
0

If none of the previous solutions worked for you.

Try this solution:

Please remove the modules imported in the imports: [] array and Use the components of the module directly in the declarations: [] array by using MockComponents(). Except the modules which contains entryComponents.

Example:

import { MockComponents } from 'ng-mocks';

    TestBed.configureTestingModule({
          imports: [
            ...,
            TranslateModule.forRoot()
          ],
         declarations: [
             myComponent,
             MockComponents(component1, component2)
         ],
          ...
          providers: [
            TranslateService
          ]
Certifiable answered 14/4, 2023 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.