PHP前端开发

在 Jest 中验证字符串

百变鹏仔 3天前 #JavaScript
文章标签 字符串

各位提交者们好!

也许您在 jest 中验证字符串时遇到了问题。



例如,上面的测试验证函数接收一个值并返回一个四舍五入的值。

it('it should properly format a unit value with rounding.', () => {    expect(formatcurrency(1234.56, true, false)).toequal({      measure: 'thousand',      value: 'r$ 1.200,00',    });  });

在这个测试中,jest 返回了一个错误,突出显示了预期值和接收到的值之间的差异,但它们是相同的 ?

- expected  - 1+ received  + 1  object {    "measure": "thousand",-   "value": "r$ 1.200,00",+   "value": "r$ 1.200,00",

解决办法是添加xa0。问题不在于你的字符串,而在于 jest 如何比较字符串值?

更正后的代码如上所示。

it('It should properly format a unit value with rounding.', () => {    expect(formatCurrency(1234.56, true, false)).toEqual({      measure: 'thousand',      value: 'R$\xa01.200,00', // remove space and add \xa0    });  });