比较toLocaleString的结果时开玩笑失败

时间:2020-01-29 15:07:28

标签: javascript unit-testing jestjs

我想测试一种使用toLocaleString的方法,但是在比较结果时,我得到一个错误Error: expect(received).toBe(expected) // Object.is equality,因为货币符号和数字之间的空白失败。

enter image description here

我已经尝试过比较它们之间的 ,并用实体代替€,但没有成功。您是否知道解决方案是什么?

我的测试:

describe('Test getFormattedCurrencyString', () => {
    test('should convert number to currency string', () => {

        const testInt = 23799.99;
        const result = testInt.toLocaleString('de-DE', {
            style: 'currency',
            currency: 'EUR',
            minimumFractionDigits: 2,
            maximumFractionDigits: 2,
        });

        expect('23.799,99 €').toBe(result);
     });
});

1 个答案:

答案 0 :(得分:1)

要了解问题所在,可以尝试

result.charCodeAt(9); // 160

这是UTF“不间断空格”,您只需从控制台复制结果并将其粘贴到单元测试中即可。

编辑,作为将来的提醒,您可以添加断言

expect(result.charCodeAt(9)).toBe(160)