Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.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
Javascript Jest |测试是否调用了矩函数以及返回输出是否正确_Javascript_Reactjs_Unit Testing_Jestjs_Enzyme - Fatal编程技术网

Javascript Jest |测试是否调用了矩函数以及返回输出是否正确

Javascript Jest |测试是否调用了矩函数以及返回输出是否正确,javascript,reactjs,unit-testing,jestjs,enzyme,Javascript,Reactjs,Unit Testing,Jestjs,Enzyme,我有一个矩函数来格式化从服务器获取的日期,以毫秒为单位。我试图用Enzyme和Jest测试矩函数是否被调用(1),以及被调用函数的输出是否是预期的 以下是我的组件: /* eslint-disable react/default-props-match-prop-types */ import React, { Fragment } from 'react'; import moment from 'moment'; import ILicense from 'user/dto/ILicense

我有一个矩函数来格式化从服务器获取的日期,以毫秒为单位。我试图用Enzyme和Jest测试
矩函数
是否被调用(1),以及被调用函数的输出是否是预期的

以下是我的组件:

/* eslint-disable react/default-props-match-prop-types */
import React, { Fragment } from 'react';
import moment from 'moment';
import ILicense from 'user/dto/ILicense';

export interface Props {
  license?: ILicense;
}

const LicenseInfo = <T extends object>({ license }: Props & T): JSX.Element => (
  <Fragment>
        <table className="table col-6 my-5">
          <tbody>
            <tr>
              <td>Valid Until:</td>
              <td className="font-weight-bold" id="expiry">
                {moment(expiredAt).format('YYYY-MM-DD HH:mm:ssZZ')}
              </td>
            </tr>
          </tbody>
        </table>
      </Fragment>
);

ComponentMoment.defaultProps = {
  expiredAt: null,
};

export default ComponentMoment;

至于第二个测试,测试实际时间,我得到一个数字而不是一个字符串,并且
toString
,不起作用

我还读了这篇关于力矩测试的文章
https://medium.com/front-end-weekly/mocking-moment-js-in-jest-a-simple-one-line-solution-61259ffaaa2
,但我真的不明白


有人能帮忙吗?我不熟悉考试。。我非常沮丧,因为我不明白失败的原因。谢谢

正如我在评论中提到的,我相信您希望使用jest.fn()创建格式,然后将其附加到矩对象


嘿,迪米特里斯。我知道这不是你问题的答案,但是你提到了你是测试新手,我想我会给你一个建议。。。一般来说,您不应该测试框架代码。相信这一刻已经考验了这一刻。。。你没有自己的逻辑去测试,谢谢。说实话,我自己也想到了。在过去的一周里,我发现自己经常处于这种情况。我知道第二个测试场景是暂时的,但是如果调用函数,是组件本身的逻辑,还是我错了?你的模拟中有额外的()=>吗?是的,我有,但这不是问题所在。错误是:
MatchError:received值必须是mock或spy
Oh。我很确定您希望格式为format:jest.fn()
  it('expect to show the expiration date if expiredAt is provided', () => {
    const moment = jest.mock('moment', () => () => ({ format: () => '2019–02–28T23:59:58' }));

    const license = {
      expiredAt: 1551391198000,
    };
    const wrapper = mount<ComponentMoment>(<ComponentMoment license={license}>Test</ComponentMoment>);
    wrapper.instance();
    expect(moment).toHaveBeenCalled();
  });
MatchError: Value must be a mock or a spy, and a huge object with various jest functions.