ngx-translate / core

The internationalization (i18n) library for Angular
MIT License
4.5k stars 574 forks source link

TypeError: this.currentLoader.getTranslation is not a function #1405

Open yesssirskii opened 1 year ago

yesssirskii commented 1 year ago

I am trying to test a component in Angular but when I try to test it I get the following
TypeError: this.currentLoader.getTranslation is not a function error.

Here is my code, if anyone can help I would highly appreciate it!!

import { HttpClient, HttpHandler } from '@angular/common/http';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { DEFAULT_LANGUAGE, MissingTranslationHandler, TranslateCompiler, TranslateLoader, TranslateModule, TranslateParser, TranslateService, TranslateStore, USE_DEFAULT_LANG, USE_EXTEND, USE_STORE } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { DialogService } from 'primeng/dynamicdialog';

import { PartyListComponent } from './party-list.component';

describe('PartyListComponent', () => {
    let component: PartyListComponent;
    let fixture: ComponentFixture<PartyListComponent>;

    beforeEach(waitForAsync(() => {
        TestBed.configureTestingModule({
            declarations: [PartyListComponent],
            imports: [RouterTestingModule, TranslateModule],
            providers: [
                HttpClient,
                HttpHandler,
                DialogService,
                TranslateService,
                TranslateStore,
                TranslateLoader,
                TranslateCompiler,
                TranslateParser,
                MissingTranslationHandler,

                { provide: USE_DEFAULT_LANG, useValue: {} },
                { provide: USE_STORE, useValue: {} },
                { provide: USE_EXTEND, useValue: {} },
                { provide: DEFAULT_LANGUAGE, useValue: {} },
            ]
        }).compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(PartyListComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });
});
bms1995 commented 1 year ago

@yesssirskii you can use TranslateFakeLoader instead of TranslateLoader it will fix your problem

nbwhub commented 11 months ago

I'm having this same error. When I add TranslateFakeloader instead, this error shows: TypeError: this.parsser.getValue is not a function....and I had to add .forRoot() to the TranslateModule in imports or I get the error "No provider for TranslateLoader!"

wavua commented 6 months ago

This

I'm having this same error. When I add TranslateFakeloader instead, this error shows: TypeError: this.parsser.getValue is not a function....and I had to add .forRoot() to the TranslateModule in imports or I get the error "No provider for TranslateLoader!"

This solution also worked for me