相关疑难解决方法(0)

Intl.NumberFormat 空格字符不匹配

我遇到了一个问题,Intl.NumberFormat即以与 Jest 期望的方式不同的方式格式化空格字符。使用不产生空格的值对同一函数进行测试,因为分隔符通过正常。Jest 正在考虑 4 和 5 字符之间的空格不同。我intl在我的应用程序和我的测试中都使用了fr_CA 的 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;
    }
  }
Run Code Online (Sandbox Code Playgroud)

我的主张:

expect(formatCurrency(24555.55, 'fr_CA', true)).toBe('24 555,55 $');
Run Code Online (Sandbox Code Playgroud)

结果: …

javascript

12
推荐指数
2
解决办法
2516
查看次数

标签 统计

javascript ×1