Jest.js test doesn't work with Intl.NumberFormat

Viewed 3896

I have a Jest test that checks the value of the input field containing currency. The number is formatted using global.Intl.NumberFormat like so:

const formatter = new global.Intl.NumberFormat(locale, config);

Where locale is set to nl-NL and config is

{ style: 'decimal',
  currency: 'EUR',
  minimumFractionDigits: 2,
  maximumFractionDigits: 2 }

When I simulate input in my test, I get the US formatting for the number, even though in my browser it works just fine. For example, if I do

input.simulate('change', { target: { value: '5000' } });

I would get '5,000.00' formatting, where for Euro I should get '5.000,00'. It does the formatting, but not for the right locale. I wonder if Jest is mocking something for me and if so how can I turn this off? Any clues would be highly appreciated.

1 Answers

You need to install https://www.npmjs.com/package/full-icu to include all locales in Node or Test environments. In browser all locales are included but in Node only english is loaded by default.

More info: https://nodejs.org/api/intl.html.

With the package installed adjust your package.json scripts: "test": "NODE_ICU_DATA=node_modules/full-icu jest"

You might also need to change scripts executed by node with node --icu-data-dir=node_modules/full-icu file, otherwise NumberFormat might behave differently on client and server.

Related