From 8d860a051ca729192f75cd2af9f5864eece09c4c Mon Sep 17 00:00:00 2001 From: anusree-bruno Date: Mon, 26 May 2025 14:43:23 +0530 Subject: [PATCH] replace real time with mocked time in faker tests --- .../src/utils/faker-functions.spec.ts | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/packages/bruno-common/src/utils/faker-functions.spec.ts b/packages/bruno-common/src/utils/faker-functions.spec.ts index 1cbc574f4..8df4eb190 100644 --- a/packages/bruno-common/src/utils/faker-functions.spec.ts +++ b/packages/bruno-common/src/utils/faker-functions.spec.ts @@ -1,14 +1,21 @@ import { mockDataFunctions } from "./faker-functions"; describe("mockDataFunctions Regex Validation", () => { - test("timestamp and isoTimestamp should return current time values", () => { - const now = Math.floor(Date.now() / 1000); - const timestamp = parseInt(mockDataFunctions.timestamp()); - const isoTimestamp = new Date(mockDataFunctions.isoTimestamp()).getTime() / 1000; + beforeAll(() => { + jest.useFakeTimers(); + jest.setSystemTime(new Date('2024-01-01T00:00:00.000Z')); + }); - // Allow for a 2-second difference to account for test execution time - expect(Math.abs(timestamp - now)).toBeLessThanOrEqual(2); - expect(Math.abs(isoTimestamp - now)).toBeLessThanOrEqual(2); + afterAll(() => { + jest.useRealTimers(); + }); + + test("timestamp and isoTimestamp should return mocked time values", () => { + const expectedTimestamp = '1704067200'; + const expectedIsoTimestamp = '2024-01-01T00:00:00.000Z'; + + expect(mockDataFunctions.timestamp()).toBe(expectedTimestamp); + expect(mockDataFunctions.isoTimestamp()).toBe(expectedIsoTimestamp); }); test("all values should match their expected patterns", () => { @@ -149,3 +156,23 @@ describe("mockDataFunctions Regex Validation", () => { } }); }); + +describe("Time-based tests", () => { + beforeAll(() => { + // Set up fake timers + jest.useFakeTimers(); + // Set a specific point in time + jest.setSystemTime(new Date('2024-01-01T00:00:00.000Z')); + }); + + afterAll(() => { + // Clean up + jest.useRealTimers(); + }); + + test("should handle time-based operations", () => { + // Your time-based tests here + const now = new Date(); + expect(now.toISOString()).toBe('2024-01-01T00:00:00.000Z'); + }); +});