MockClock Unit Testing

MockClock Unit Testing

MockClock Unit Testing

Kacper Walczak · 20-08-2024

  • Mock time to check wheter product can be returned in tests within 30 days threshold.

Introduction

Context: Let's assume, that we have function isReturnEligible, which check, whether given product can be returned/claimed (eg. 30 days since buy). Function bases on current date, to determine, wheter client can return product.

Function to test: At the beginning, let's create function isReturnEligible:

type Order = {
  purchaseDate: Date;
};
 
function isReturnEligible(order: Order, currentDate: Date): boolean {
  const returnPeriod = 30; // max 30 days to 
  const purchaseTime = order.purchaseDate.getTime();
  const currentTime = currentDate.getTime();
  
  const diffInDays = (currentTime - purchaseTime) / (1000 * 60 * 60 * 24);
  
  return diffInDays <= returnPeriod;
}

Unit test that uses MockClock

We will write unit test that utilizes MockClock to simulate time that pass.

We used Jest, to showcase unit test below:

class MockClock {
  private currentTime: Date;
 
  constructor(initialTime?: Date) {
    this.currentTime = initialTime ? new Date(initialTime) : new Date();
  }
 
  now(): Date {
    return new Date(this.currentTime);
  }
 
  setTime(newTime: Date): void {
    this.currentTime = new Date(newTime);
  }
 
  advanceTimeBy(milliseconds: number): void {
    this.currentTime = new Date(this.currentTime.getTime() + milliseconds);
  }
 
  advanceTimeByDays(days: number): void {
    this.advanceTimeBy(days * 24 * 60 * 60 * 1000);
  }
}
 
describe('isReturnEligible', () => {
  let mockClock: MockClock;
  const purchaseDate = new Date('2024-01-01T00:00:00Z');
 
  beforeEach(() => {
    mockClock = new MockClock(purchaseDate);
  });
 
  test('Product is qualified to be returned within 30 days threshold', () => {
    const order: Order = { purchaseDate };
 
    // Set clock to 15 days after purchase
    mockClock.advanceTimeByDays(15);
    expect(isReturnEligible(order, mockClock.now())).toBe(true); // Return is possible
  });
 
  test('Product is not qualified to be returned within 30 days threshold', () => {
    const order: Order = { purchaseDate };
 
    // Set clock to 31 days after purchase
    mockClock.advanceTimeByDays(31);
    expect(isReturnEligible(order, mockClock.now())).toBe(false); // Return is not possible
  });
 
  test('Product is qualified to be returned within exactly 30 days threshold', () => {
    const order: Order = { purchaseDate };
 
    // Set clock to be specific 30 days after purchase
    mockClock.advanceTimeByDays(30);
    expect(isReturnEligible(order, mockClock.now())).toBe(true); // Return is possible
  });
});

Next

In this article we have learned how to and why use MockClock in you Unit tests.

Check next:

READ

Latest readings

  • Readings are sites which will help you with detailed

  • information about given topic. Read latest ones from Learn.

AI

06-03-2026

Local Voice Assistant with Ollama
  • Build your own local voice assistant powered by Ollama.

AI

06-03-2026

AI YouTube Thumbnail Generator
  • Generate YouTube thumbnails with FastAPI and Ollama.

Architecture

05-09-2024

Graph DB usage comparison
  • Compare Neo4j and Tigergraph databases, which is easier to work with, etc.