I'm running into an issue where Intl.NumberFormat
is formatting the space character in some way that is different from what Jest is expecting. Tests against the same function with values that do not yield spaces as separators pass fine. Jest is considering the space between the 4 and the 5 characters to be different. I am using the intl
polyfill for fr_CA both in my app and in my test.
Here is my function, but the only really relevant part is the Intl.NumberFormat
output. How can I reconcile the formatting for the space character so that my test passes?
export function formatCurrency( num, locale = 'en_US', showFractionDigits = false) {
const options = {
style: locale === 'fr_CA' ? 'decimal' : 'currency',
currency: locale.length && locale.indexOf('CA') > -1 ? 'CAD' : 'USD'
};
const final = new Intl.NumberFormat(locale.replace('_', '-'), options).format(num);
if (locale === 'fr_CA') {
return `${final} $`;
} else {
return final;
}
}
My assertion:
expect(formatCurrency(24555.55, 'fr_CA', true)).toBe('24 555,55 $');
Result:
Expected value to be:
"24 555,55 $"
Received:
"24 555,55 $"