Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.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 使用Luxon的Jest单元测试:如何模拟.setZone(';local';)_Reactjs_Unit Testing_Jestjs_Luxon - Fatal编程技术网

Reactjs 使用Luxon的Jest单元测试:如何模拟.setZone(';local';)

Reactjs 使用Luxon的Jest单元测试:如何模拟.setZone(';local';),reactjs,unit-testing,jestjs,luxon,Reactjs,Unit Testing,Jestjs,Luxon,我有一个非常简单的日期组件: import { DateTime } from 'luxon'; export const SimpleDateCell = ({ item, itemKey }) => { const isoDateString = item[itemKey]; if (!isoDateString) { return null; } const formattedDate = DateTime .fro

我有一个非常简单的日期组件:

import { DateTime } from 'luxon';
export const SimpleDateCell = ({ item, itemKey }) => {
    const isoDateString = item[itemKey];

    if (!isoDateString) {
        return null;
    }

    const formattedDate = DateTime
        .fromISO(isoDateString, { zone: 'utc' })
        .setZone('local')
        .toLocaleString(DateTime.DATETIME_MED);

    return (
        <time dateTime={isoDateString} title={isoDateString}>
            {formattedDate}
        </time>
    ); };
从'luxon'导入{DateTime};
导出常量SimpleDateCell=({item,itemKey})=>{
const isoDateString=item[itemKey];
如果(!isoDateString){
返回null;
}
const formattedDate=DateTime
.fromISO(isoDateString,{zone:'utc'})
.setZone(“本地”)
.tolocalesting(DateTime.DateTime\u-MED);
返回(
{formattedDate}
); };
我在本地用以下方法进行测试:

import React from 'react'; 
import SimpleDateCell from './SimpleDateCell'; 
import { DateTime, Settings } from 'luxon';
import renderer from 'react-test-renderer';

const testData = {
    companyName: 'test company',
    tenantId: 'ai/prod/00DA0000000XoMwMAK',
    // this will be different on different servers when .setZone('local')
    createdDate: '2019-02-13T15:53:00', };

describe('SimpleDateCell', () => {
    it('should match the snapshot', () => {


        const tree = renderer
            .create(
                <SimpleDateCell item={testData} itemKey="createdDate" />
            ).toJSON();
        expect(tree).toMatchSnapshot();
    });  });
从“React”导入React;
从“./SimpleDateCell”导入SimpleDateCell;
从“luxon”导入{DateTime,Settings};
从“反应测试渲染器”导入渲染器;
常数testData={
公司名称:“测试公司”,
租户ID:'ai/prod/00DA0000000XoMwMAK',
//当.setZone('local')运行时,不同服务器上的情况会有所不同
createdDate:'2019-02-13T15:53:00',};
描述('SimpleDateCell',()=>{
它('应该匹配快照',()=>{
常量树=渲染器
.创造(
).toJSON();
expect(tree.toMatchSnapshot();
});  });
问题是,单元测试必须在创建快照的同一时区中运行。所以我们的CI服务器拒绝了这个

有没有办法在任何时区进行此传递?可能会模拟设置区域(“本地”)响应?有没有CI专家使用luxon

谢谢大家!

应该有效。以防我强调:它影响所有方法,比如
DateTime.local()
。有人被它吓坏了


作为替代,您可以用或模仿本机
日期
,我认为这也会有所帮助。如果出于任何原因,您的代码的某些部分与本机的
Date
而不是luxon的
DateTime

一起工作,那么这种方法会更加一致。您尝试过吗?成功了!!非常感谢。你让我在我的团队中显得非常聪明。我鼓励你把它作为官方的回应。我想得到别人的信任!官方的回答是:)哦,我花了好几个小时来模拟约会时间。出于某种原因,模拟链接模块的示例对我还不起作用,所以我很高兴收到您的设置建议。对于其他人来说,这是@skyboyer响应后我的测试:description('SimpleDateCell',()=>{beforeAll(()=>{//,以便在测试设置中不使用local.defaultZoneName='utc';});毕竟(()=>{//,返回local Settings.defaultZoneName='local';});它('should match the snapshot',()=>{const tree=renderer.create().toJSON();expect(tree.toMatchSnapshot();});});