Skip to content

Commit

Permalink
fix: impl milliseconds for test args
Browse files Browse the repository at this point in the history
Using dayjs add/subtract methods with "day" units made the date calculations dependent on the time of execution — by converting to milliseconds, every test is now deterministic.
  • Loading branch information
trevor-anderson committed Mar 9, 2024
1 parent 7598b2b commit 9e68ce1
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/utils/numeric/calculate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ const getTimestampInMultipleFormats = (dayjsTimestampObject: dayjs.Dayjs) => {
};

describe("numeric/calculate", () => {
const NOW_MILLISECONDS = Date.now();
const NUM_MILLISECONDS_PER_DAY = 86400000;

// SHARED TIMESTAMP INPUTS FOR `getTimestampAge()` AND `getItemAge()`:

const threeDaysAgo = dayjs().subtract(3, "days");
const threeDaysAgo = dayjs(NOW_MILLISECONDS - NUM_MILLISECONDS_PER_DAY * 3);
const threeDaysAgoTimestamps = getTimestampInMultipleFormats(threeDaysAgo);

const fiveDaysAgo = dayjs().subtract(5, "days");
const fiveDaysAgo = dayjs(NOW_MILLISECONDS - NUM_MILLISECONDS_PER_DAY * 5);
const fiveDaysAgoTimestamps = getTimestampInMultipleFormats(fiveDaysAgo);

const threeDaysFromNow = dayjs().add(3, "day");
const threeDaysFromNow = dayjs(NOW_MILLISECONDS + NUM_MILLISECONDS_PER_DAY * 3);
const threeDaysFromNowTimestamps = getTimestampInMultipleFormats(threeDaysFromNow);

// prettier-ignore
Expand All @@ -47,10 +50,10 @@ describe("numeric/calculate", () => {

test("returns a negative number when the provided timestamp is in the future", () => {
threeDaysFromNowTimestamps.forEach((threeDaysFromNowTS) => {
// The resulting decimals are truncated to integers, hence -71 and -2 instead of -72 and -3
// The resulting decimals are truncated to integers, hence -71 instead of -72
expect(getTimestampAge(threeDaysFromNowTS, "hours")).toBe(-71);
expect(getTimestampAge(threeDaysFromNowTS, "days")).toBe(-2);
expect(getTimestampAge(threeDaysFromNowTS)).toBe(-2);
expect(getTimestampAge(threeDaysFromNowTS, "days")).toBe(-3);
expect(getTimestampAge(threeDaysFromNowTS)).toBe(-3);
expect(getTimestampAge(threeDaysFromNowTS, "months")).toBe(0);
expect(getTimestampAge(threeDaysFromNowTS, "years")).toBe(0);
});
Expand Down Expand Up @@ -87,8 +90,8 @@ describe("numeric/calculate", () => {
// Use the TS to define the test item's `createdAt` property:
const itemWithTS = { createdAt: threeDaysFromNowTS };
expect(getItemAge(itemWithTS, "hours")).toBe(-71);
expect(getItemAge(itemWithTS, "days")).toBe(-2);
expect(getItemAge(itemWithTS)).toBe(-2);
expect(getItemAge(itemWithTS, "days")).toBe(-3);
expect(getItemAge(itemWithTS)).toBe(-3);
expect(getItemAge(itemWithTS, "months")).toBe(0);
expect(getItemAge(itemWithTS, "years")).toBe(0);
});
Expand Down

0 comments on commit 9e68ce1

Please sign in to comment.