【问题标题】:How to use moment in jest react unit test?如何在开玩笑反应单元测试中使用时刻?
【发布时间】:2019-01-08 10:15:03
【问题描述】:

我有一个 Unit 组件,它使用 moment.js 设置日期格式:

let bdate= moment(this.props.person.birthDate).format(
                'hh:mm A, DD MMM YYYY'
              );

稍后此 bdate 会在组件中呈现。

这个组件有一个单元测试:

describe('Unit', () => {
  it('should render correctly', () => {
    const person = 
      {      
        birthDate: '2018-06-14T00:00:00.000Z',
      }

    const wrapper = shallow(
      <Unit
        person={person}
      />
    );
    expect(wrapper).toMatchSnapshot();
  });
});

问题是 Unit 的快照输出当前日期?为什么不是模拟日期?我该如何解决这个问题,以便快照保持不变?

【问题讨论】:

  • 你能分享Unit的代码吗?

标签: reactjs momentjs jestjs


【解决方案1】:

我为此使用了mockdate util。它允许您模拟测试的“现在”值。

【讨论】:

    猜你喜欢
    • 2021-08-27
    • 2015-06-28
    • 2017-01-25
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 2017-10-01
    • 2018-09-10
    • 2021-12-29
    相关资源
    最近更新 更多