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?
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:
npm i -D full-icunode --icu-data-dir=node_modules/full-icu node_modules/jest/bin/jest.js
Most helpful comment
@icambron Seems like you're right. Fixed it with:
npm i -D full-icunode --icu-data-dir=node_modules/full-icu node_modules/jest/bin/jest.js