【问题标题】:How to tell Jest that spaces are in fact, spaces?如何告诉 Jest 空间实际上是空间?
【发布时间】:2021-05-30 05:05:29
【问题描述】:

给定代码:

import { getLocale } from './locale';

export const euro = (priceData: number): string => {
  const priceFormatter = new Intl.NumberFormat(getLocale(), {
    style: 'currency',
    currency: 'EUR',
  });

  return priceFormatter.format(priceData);
}

export default null;

及相关测试:

import { euro } from './currency';

test('euro', () => {
  expect(euro(42)).toBe("42,00 €");
});

开玩笑说:

即使我将 Jest 的预期结果复制粘贴到我的断言中,错误仍然是一样的。

所以问题是:为什么? :-D

【问题讨论】:

标签: node.js typescript jestjs currency number-formatting


【解决方案1】:

您希望此测试断言:

"42,00\xa0€"

它不是空格(不同的 ascii 代码/unicode)。根据a jest issue about string comparison being incorrectIntl.NumberFormat 使用了一个不间断的空格。

正如in a similar question's answer指出的那样:

NumberFormat 使用小的不间断空格 (\u202f) 作为千位分隔符和普通不间断空格 (\xa0)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-18
    • 2016-02-05
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 2013-12-22
    相关资源
    最近更新 更多