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

后端 未结 7 2098
暗喜
暗喜 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:28

    The easiest way to mock moment() and any function that it uses (i.e. .day(), .format()) is to change the Date that moment() uses under the hood

    Add the snippet below inside your test file

    Date.now = jest.fn(() => new Date("2020-05-13T12:33:37.000Z"));

    This makes it so anytime moment() is called in your tests, moment() thinks that today is Wednesday, May 13th 2020

提交回复
热议问题