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

后端 未结 7 2223
暗喜
暗喜 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:29

    mockdate works for me

    import mockDate from "mockdate";
    
    
    test('Should add some', () => {
        mockDate.set(new Date('2/20/2020'));
    
        const action = addSome();
    
        expect(action).toEqual({
            createdAt: moment()
        });
    
        mockDate.reset();
    })
    

提交回复
热议问题