Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intl.NumberFormat behaves incorrectly in Jest unit test?

The Mozilla site says:

var number = 123456.789;

console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' })
.format(number));

// expected output: "123.456,79 €"

But in my Jest unit test, I get as output € 123,456.79 which is not correct for fr-FR locale according to me and Mozilla example.

I've tried to load polyfills and locale data, but it does not seem to resolve the issue

import 'intl';
import 'intl/locale-data/complete';
import 'intl/locale-data/jsonp/fr';
import 'intl/locale-data/jsonp/fr-FR';
import 'intl/dist/Intl.complete';

Any idea what could be wrong?

like image 456
Sebastien Lorber Avatar asked Sep 14 '18 10:09

Sebastien Lorber


2 Answers

A solution that worked for me is:

  1. npm i full-icu
  2. Run your test with NODE_ICU_DATA=node_modules/full-icu jest
like image 142
Alex Zinkevych Avatar answered Sep 17 '22 20:09

Alex Zinkevych


For anyone interested, Node >= 13 has full-icu support enabled by default: https://nodejs.medium.com/node-js-12-to-lts-and-node-js-13-is-here-e28d6a4a2bd

like image 22
Sebastien Lorber Avatar answered Sep 21 '22 20:09

Sebastien Lorber