Reactjs Jest测试:自定义货币/十进制格式化程序显示错误的分隔符
我尝试在React应用程序内部切换到Jest测试。Reactjs Jest测试:自定义货币/十进制格式化程序显示错误的分隔符,reactjs,decimal,delimiter,jestjs,Reactjs,Decimal,Delimiter,Jestjs,我尝试在React应用程序内部切换到Jest测试。 很多摩卡咖啡和酶测试都是用小的方法进行的 对于货币格式化组件的测试,我在测试中得到了错误的区域性格式。我希望结果的格式如下: 1.234.567,89欧元 但我收到: 1234567.89欧元 在这个应用程序中,我得到了所有的值,如预期的那样以德语格式很好地格式化 顺便说一句: 在助手方法内部,我们使用以下代码进行内部格式化 value.toLocaleString(['de-DE'], { style: 'decimal',
很多摩卡咖啡和酶测试都是用小的方法进行的 对于货币格式化组件的测试,我在测试中得到了错误的区域性格式。我希望结果的格式如下: 1.234.567,89欧元 但我收到: 1234567.89欧元 在这个应用程序中,我得到了所有的值,如预期的那样以德语格式很好地格式化 顺便说一句:
在助手方法内部,我们使用以下代码进行内部格式化
value.toLocaleString(['de-DE'], {
style: 'decimal',
useGrouping: true,
minimumFractionDigits: 2,
maximumFractionDigits: 2
})
我知道了
因为Jest使用Node进行测试,并且Node不支持“en”以外的其他语言环境,所以我需要在Jest设置文件中添加Intl/IntlPolyfill
const areIntlLocalesSupported = require('intl-locales-supported');
const localesMyAppSupports = [
'de'
];
if (global.Intl) {
// Determine if the built-in `Intl` has the locale data we need.
if (!areIntlLocalesSupported(localesMyAppSupports)) {
// `Intl` exists, but it doesn't have the data we need, so load the
// polyfill and patch the constructors we need with the polyfill's.
const IntlPolyfill = require('intl');
Intl.NumberFormat = IntlPolyfill.NumberFormat;
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
Number.prototype.toLocaleString = IntlPolyfill.__localeSensitiveProtos.Number.toLocaleString;
Date.prototype.toLocaleString = IntlPolyfill.__localeSensitiveProtos.Date.toLocaleString;
}
} else {
// No `Intl`, so use and load the polyfill.
global.Intl = require('intl');
}
我不知道原因,但我需要显式覆盖Number.prototype.tolocalString
和Date.prototype.tolocalString