Components using Date objects produce different snapshots in different timezones

后端 未结 5 669
野趣味
野趣味 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
    慢半拍i (楼主)
    2020-12-05 04:35

    I ended up getting around this by mocking the toLocaleString (or whatever toString method you are using) prototype. Using sinon I did:

    var toLocaleString;
    
    beforeAll(() => {
        toLocaleString = sinon.stub(Date.prototype, 'toLocaleString', () => 'fake time')
    })
    
    afterAll(() => {
        toLocaleString.restore()
    })
    

    This way if you are generating strings straight from a Date object, you're still OK.

提交回复
热议问题