Roger Ramos grayscale profile photo with a nice smile and hat

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', () => {


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. 😂

Go back