fix: ensure timestamp and isoTimestamp return current time instead of random values

This commit is contained in:
anusree-bruno
2025-05-15 14:11:53 +05:30
parent 942c0ee113
commit d2eb2d2941
2 changed files with 13 additions and 3 deletions

View File

@@ -1,10 +1,20 @@
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;
// 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);
});
test("all values should match their expected patterns", () => {
const patterns: Record<string, RegExp> = {
guid: /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/,
timestamp: /^\d{13,}$/,
timestamp: /^\d{10}$/,
isoTimestamp: /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/,
randomUUID: /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/,
randomAlphaNumeric: /^[\w]$/,

View File

@@ -2,8 +2,8 @@ import { faker } from '@faker-js/faker';
export const mockDataFunctions = {
guid: () => faker.string.uuid(),
timestamp: () => faker.date.anytime().getTime().toString(),
isoTimestamp: () => faker.date.anytime().toISOString(),
timestamp: () => Math.floor(Date.now() / 1000).toString(),
isoTimestamp: () => new Date().toISOString(),
randomUUID: () => faker.string.uuid(),
randomAlphaNumeric: () => faker.string.alphanumeric(),
randomBoolean: () => faker.datatype.boolean(),