Locale files for unit test in Angular 5

Viewed 3056

how do you solve missing locale from CLDR in angular 5 in unit tests?

I have in app.module registerLocaleData(...) and it works for the app, however when I run unit tests where I use pipes with locale, it doesn't know about app.module.

It sounds weird to load these locales in each test. I tried to import it in test.ts but with no luck.

Any ideas?

1 Answers

I had the same issue. Fixed it by modifying test.ts like so:

...
// Add these two imports
import { registerLocaleData } from '@angular/common';
import localeDe from '@angular/common/locales/de';

...

getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);

// Add this line to register a locale (german in this case).
registerLocaleData(localeDe);
Related