【发布时间】:2019-06-12 00:20:07
【问题描述】:
我遇到了一个问题,Intl.NumberFormat 正在以某种不同于 Jest 所期望的方式格式化空格字符。使用不产生空格作为分隔符的值对同一函数进行测试可以正常通过。 Jest 正在考虑 4 和 5 个字符之间的空间是不同的。我在我的应用程序和测试中都在为 fr_CA 使用 intl polyfill。
这是我的函数,但唯一真正相关的部分是 Intl.NumberFormat 输出。如何协调空格字符的格式以便我的测试通过?
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;
}
}
我的主张:
expect(formatCurrency(24555.55, 'fr_CA', true)).toBe('24 555,55 $');
结果:
Expected value to be:
"24 555,55 $"
Received:
"24 555,55 $"
【问题讨论】:
-
我看不出您的预期和收到的差异。你那里有错字吗?
-
没有错字。这就是这个问题的全部意义所在。
标签: javascript