Components using Date objects produce different snapshots in different timezones

后端 未结 5 670
野趣味
野趣味 2020-12-05 04:25

I\'m using Enzyme with enzyme-to-json to do Jest snapshot testing of my React components. I\'m testing shallow snapshots of a DateRange component that renders a

5条回答
  •  被撕碎了的回忆
    2020-12-05 04:52

    If you're using new Date() constructor instead of Date.now you can do like below:

    const RealDate = Date;
    
    beforeEach(() => {
      // @ts-ignore
      global.Date = class extends RealDate {
        constructor() {
          super();
          return new RealDate("2016");
        }
      };
    })
    afterEach(() => {
      global.Date = RealDate;
    });
    

    This issue is a must visit if you're here.

提交回复
热议问题