Intl.NumberFormat behaves incorrectly in Jest unit test?

Viewed 7584

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?

5 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

Looks like an inconsistency between node and browser behavior. I think polyfills might not have worked, because your node has support for Intl. And there is a check to avoid overriding the native implementation. Try using IntlPolyfill, it should be available. I also tried node 6.14.4, and got €123,456.79 :D

Please check node version first before other trials, because my previous node version was v10.16.0, and I have updated to v12.12.3 which is working for me.

And also, please add this currentDisplay: 'narrowSymbol' in your number format object, something like this:

{Intl.NumberFormat('en-AU', { style: 'currency', currency: 'AUD', currencyDisplay: 'narrowSymbol' }).format(
          (amount)
        )}
Related