Components using Date objects produce different snapshots in different timezones

后端 未结 5 660
野趣味
野趣味 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:30

    I struggled with this for hours/days and only this worked for me:

    1) In your test:

    Date.now = jest.fn(() => new Date(Date.UTC(2017, 7, 9, 8)).valueOf())
    

    2) Then change the TZ env var before running your tests. So the script in my package.json:

    • (Mac & Linux only)

      "test": "TZ=America/New_York react-scripts test --env=jsdom",
      
    • (Windows)

      "test": "set TZ=America/New_York && react-scripts test --env=jsdom",
      

提交回复
热议问题