Search code examples
angularunit-testingdatelocale

Testing getLocaleFirstDayOfWeek


Swedish week starts on Monday. Used getLocaleFirstDayOfWeek to fetch the first day of the week. When testing, it kept providing Sunday as the first day of the week. It required the test to register the local data to work correctly. Is this a defect or as expected?

import { getLocaleFirstDayOfWeek, registerLocaleData, WeekDay } from '@angular/common';
import EN_SE from '@angular/common/locales/en-SE';

describe('getLocaleFirstDayOfWeek', () => {
  beforeAll(() => registerLocaleData(EN_SE, 'en-SE'));

  fit('should start the week on monday', () => {
    const firstDayOfWeek = getLocaleFirstDayOfWeek('en-SE');
    expect(firstDayOfWeek).toEqual(WeekDay.Monday);
  });
});

Solution

  • Okay found the answer! With Angular 5 onwards only the en-US is included by default, any additional locales need to be registered via registerLocaleData.

    By default Angular now only contains locale data for the language en-US, if you set the value of LOCALE_ID to another locale, you will have to import new locale data for this language because we don’t use the intl API anymore.