Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Jest测试:自定义货币/十进制格式化程序显示错误的分隔符_Reactjs_Decimal_Delimiter_Jestjs - Fatal编程技术网

Reactjs 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',

我尝试在React应用程序内部切换到Jest测试。
很多摩卡咖啡和酶测试都是用小的方法进行的

对于货币格式化组件的测试,我在测试中得到了错误的区域性格式。我希望结果的格式如下:

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