Skip to content
Myr

add-months-clamp.test.ts

This file

Path

test/asks/add-months-clamp.test.ts

20 lines · 1.0 KiB · TypeScript

Last changed by

opened add-months-clampby ines-vb553230

History of this file

Take it

1import { expect, test } from "bun:test";23import { addMonths, addYears } from "../../src/index.ts";45const plusMonths = (text: string, amount: number) =>6  addMonths(new Date(text), amount).toISOString();78test("addMonths ends on the last day of a target month too short for the day", () => {9  // January 31 plus a month is the end of February, in leap and common years.10  expect(plusMonths("2028-01-31T00:00:00Z", 1)).toBe("2028-02-29T00:00:00.000Z");11  expect(plusMonths("2026-01-31T00:00:00Z", 1)).toBe("2026-02-28T00:00:00.000Z");12  // The time of day stays, and so does the direction.13  expect(plusMonths("2026-10-31T09:30:00Z", 1)).toBe("2026-11-30T09:30:00.000Z");14  expect(plusMonths("2026-03-31T00:00:00Z", -1)).toBe("2026-02-28T00:00:00.000Z");15  expect(plusMonths("2026-05-31T00:00:00Z", -11)).toBe("2025-06-30T00:00:00.000Z");16  // A year is twelve months, so February 29 plus a year is February 28.17  expect(addYears(new Date("2028-02-29T00:00:00Z"), 1).toISOString()).toBe(18    "2029-02-28T00:00:00.000Z",19  );20});