Jasmine date mocking with moment.js

后端 未结 3 614
忘了有多久
忘了有多久 2021-02-18 14:07

I\'m using moment.js for date/time in my application, but it seems like it doesn\'t play well with Jasmine\'s mocking capabilities. I\'ve put together a test suite below that sh

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-18 14:31

    I was trying to find an alternative to jasmine or even other mock frameworks to avoid dependencies.

    const currentToday = moment().toDate();
    console.log(`currentToday:`, currentToday)
    
    const newToday = moment('1980-01-01').toDate();
    console.log(`newToday    :`, newToday);
    
    Date.now = () => {
      return newToday
    };
    
    const fakedToday = moment().toDate();
    console.log(`fakedToday  :`, fakedToday)

    currentToday: 2019-09-17T15:26:12.763Z
    newToday    : 1980-01-01T00:00:00.000Z
    fakedToday  : 1980-01-01T00:00:00.001Z
    

提交回复
热议问题