Testing { Angular 2 + Webpack + Karma + Jasmine + ng2-Translate }

Hi HashNoders.,

I have an Angular 2 ( Typescript + Webpack + Karma + Jasmine ) application and it has some basic test cases running green.

After sometime of browsing, ng2-translate seems the best library for Angular 2 Translation as of now, until Angular Team comes up with their own.

Translation worked perfect for me with that library. But, all my unit tests are blood red now :(

I am not clear on how to mock/provide Dependency Injection of the Translation Service in each Component's constructor and Translation Pipes in templates. I wanted to know how to do this, in general as well.

(The link to the library has some sample code on usage and stuffs, but not on how to unit test.)

Appreciate your help in advance.

Comments (1)

Patrick O'Neill's photo

Having the exact same issue. In my TestBed config I added this to the providers.

          provide: TranslateLoader,
          useFactory: (http: Http) => new TranslateStaticLoader(http, '/assets/i18n', '.json'),
          deps: [Http]

and then in my test

  it('should create the app', inject([TranslateService], (translate) => {
    let fixture = TestBed.createComponent(AppComponent);
    let app = fixture.debugElement.componentInstance;

However, my karma test server returns a 404 for en.json. Proxies in karma conf seem to do shag all. So frustrating as I know the solution is probably very simple.