Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locale files for unit test in Angular 5

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?

like image 668
Konrad Cerny Avatar asked Nov 07 '17 13:11

Konrad Cerny


People also ask

Where do I put unit test files?

Unit tests run against specific lines of code. So it makes sense to place them right next to that code. Integration tests run against many lines of code in many files. There is no single place that would make sense, so it's best to have them in a /tests directory.

What is locale in Angular?

Angular uses the Unicode locale identifier (Unicode locale ID) to find the correct locale data for internationalization of text strings. Unicode locale ID. A locale ID conforms to the Unicode Common Locale Data Repository (CLDR) core specification.

What is Spec TS file in Angular?

spec. ts file. This file contains unit tests for the main AppComponent. When running tests using the Angular CLI, all unit tests in files with the *. spec.


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);
like image 97
Pascal Chorus Avatar answered Sep 30 '22 13:09

Pascal Chorus