Mocking moment() and moment().format using jest

后端 未结 7 2097
暗喜
暗喜 2021-02-12 19:46

I\'m unable to mock moment() or moment().format functions. I have states where, currentDateMoment and currentDateFormatted ar

相关标签:
7条回答
  • 2021-02-12 20:41

    I was still getting some errors because I was using moment-timezone, too. So, here's what I did to fix that:

    let diffMins = updateThreshold + 1;
    jest.mock('moment', () => {
      const mMoment = {
        diff: jest.fn(() => diffMins),
      };
      const fn = jest.fn(() => mMoment);
      fn.version = '2.24';
      fn.tz = jest.fn();
      fn.fn = jest.fn();
      return fn;
    });

    0 讨论(0)
提交回复
热议问题