angularlocalekarma-runner

How to Unit test registerLocaleData in angular


I am trying to test the actual output from a date string conversion with angular 8, and Karma.

In my app.module I use the syntax: registerLocaleData(localeNl, 'nl');

In my template I use {{date | date: 'MMMM yyyy'}}

When I unit test the pipe conversion with :

element = fixture.nativeElement;
content = element.querySelector('.content');
expect(content.innerText).toContain('mei 2020')

I get the Karma error that the innerText is "May 2020" and not "mei 2020" but in the actual angular application it renders the text correctly "mei 2020". So How can I get the Karma environment to use the same locale as the application?


Solution

  • Try providing it:

    import { LOCALE_ID } from '@angular/core';
    import { registerLocaleData } from '@angular/common';
    ...
    registerLocaleData(localeNL, 'nl');
    ...
    TestBed.configureTestingModule({
       providers: [{ provide: LOCALE_ID, useValue: 'nl' }], // you know the useValue here
    });
    
    

    As for the currency being . instead of ,, I have no idea why that is happening.