1

When testing an IntL money conversion inside Jest, I don't get the correct conversion.

My test:

expect(new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' }).format(50.00)).toBe('R$ 50,00');
// Expected value to be:
// "R$ 50,00"
// Received:
// "R$ 50.00"

My command:

jest --config test/unit/jest.conf.js

How can I set the correct locale configuration to Jest?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
jotafeldmann
  • 642
  • 10
  • 17

2 Answers2

9

UPDATE: Full ICU now comes with Node.js, since version 13.


Original answer:

The challenge here is to set Node.js i18n, Jest is Node.js based.

Accordingly, with official Node.js documentation, we must set the full-icu parameter.

npm install full-icu
NODE_ICU_DATA=node_modules/full-icu jest --config jest.conf.js

Now the test is correct.

jotafeldmann
  • 642
  • 10
  • 17
  • 2
    is there any way to do this inside of the jest setup files? – Kelly Milligan Jan 11 '19 at 21:15
  • @KellyMilligan I don't see how it's possible. Here we got an explicit argument about ICU, before Node.js loading. Inside Jest configuration, you are declaring something to be loaded after Node.js... – jotafeldmann May 09 '19 at 02:25
  • @JasonRice good question. You need to find some way to dynamically set the `NODE_ICU_DATA` before executing jest. Maybe that link can help https://stackoverflow.com/questions/9249830/how-can-i-set-node-env-production-on-windows. – jotafeldmann May 09 '19 at 02:30
  • 1
    I ended up using cross-env from NPM and then set the following for our stage and production server: `cross-env NODE_ENV=production node --icu-data-dir=node_modules/full-icu -r esm src/server/index.js` This works really well. Then within your local dev environment simply add the following to your .env: `NODE_ICU_DATA=node_modules/full-icu` – Jason Rice May 10 '19 at 14:40
0

Probably in Brazil use comma sign, to separate decimal numbers. Instead of dot sign .. Here is the link to world map which indicates which country, uses which decimal indicator. http://www.statisticalconsultants.co.nz/blog/how-the-world-separates-its-decimals.html

Talgat Saribayev
  • 1,898
  • 11
  • 19