Luxon: Default locale doesn't work in jest tests

Created on 18 Sep 2019  路  2Comments  路  Source: moment/luxon

I'm writing tests to see an expected time from a particular component, but tests don't recognize default locale.

Example:

import { DateTime, Settings } from 'luxon';

test('locale is de -> should return time in correct format', () => {
    Settings.defaultLocale = 'de';

    const time = DateTime.fromMillis(13 * 60 * 60 * 1000, { zone: 'utc' })
                         .toLocaleString({ hour: '2-digit', minute: '2-digit' });

    expect(time).toEqual('01:00 PM'); // Should be 13:00
});

What am I doing wrong?

troubleshooting

Most helpful comment

@icambron Seems like you're right. Fixed it with:

  1. npm i -D full-icu
  2. node --icu-data-dir=node_modules/full-icu node_modules/jest/bin/jest.js

All 2 comments

My guess is that your node env doesn't have the ICU data and is falling back to en-US. You can try a test with setting the locale directly on the instance or even using the Intl API directly. I suspect they'll fail the same way, and it's not about defaults, just about the availability of intl in your test env generally. If not, I'll look closer.

Fwiw, Luxon itself has a test for this.

@icambron Seems like you're right. Fixed it with:

  1. npm i -D full-icu
  2. node --icu-data-dir=node_modules/full-icu node_modules/jest/bin/jest.js
Was this page helpful?
0 / 5 - 0 ratings

Related issues

zbraniecki picture zbraniecki  路  6Comments

UnsungHero97 picture UnsungHero97  路  5Comments

ilyazub picture ilyazub  路  5Comments

thetuyen picture thetuyen  路  4Comments

dnalborczyk picture dnalborczyk  路  3Comments