rogerramos.me

Mocking Date in Jest

Someday I had to test a function that uses the Date to show or not a component. Well, that's not too simple as I've imagined, but after some googling, I manage to get it done with this issue.

The Problem

Test cases with Date manipulation

The Solution

describe('getTimestamp', () => {
  const RealDate = Date

  function mockDate (isoDate) {
    global.Date = class extends RealDate {
      constructor () {
        return new RealDate(isoDate)
      }
    }
  }

  afterEach(() => {
    global.Date = RealDate
  })

  it('should return timestamp', () => {
    mockDate('2017-11-25T12:34:56z')
    expect(getTimestamp()).toEqual('20171125123456')
  })
})

Conclusion

There are a lot of discussions about the most underground scenarios and some are difficult to find. If I could give you advice, just be patient. 😂

javascriptjestdate
Go back