Components using Date objects produce different snapshots in different timezones

后端 未结 5 661
野趣味
野趣味 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条回答
  •  -上瘾入骨i
    2020-12-05 04:45

    I did this by using timezone-mock, it internally replaces the global Date object and it's the easiest solution I could find.

    The package supports a few test timezones.

    import timezoneMock from 'timezone-mock';
    
    describe('when in PT timezone', () => {
      beforeAll(() => {
        timezoneMock.register('US/Pacific');
      });
    
      afterAll(() => {
        timezoneMock.unregister();
      });
    
      // ...
    

    https://www.npmjs.com/package/timezone-mock

提交回复
热议问题