Skip to content
Myr

format.test.ts

This file

Path

test/format.test.ts

95 lines · 3.6 KiB · TypeScript

Last changed by

landed by oscabriel6a2b761

History of this file

Take it

landed by oscabriel6a2b761

lines 1–95
1import { describe, expect, test } from "bun:test";
23import { format, isTimeZone, offsetIn } from "../src/index.ts";45// Wednesday, October 7, 2026, 14:05:09.250 UTC.6const afternoon = new Date(Date.UTC(2026, 9, 7, 14, 5, 9, 250));78const on = (year: number, month: number, day: number, hour = 0, minute = 0) =>9 new Date(Date.UTC(year, month - 1, day, hour, minute));1011describe("format", () => {12 test("shows the year, month and day by their tokens", () => {13 expect(format(afternoon, "yyyy-MM-dd")).toBe("2026-10-07");14 expect(format(afternoon, "d MMMM yyyy")).toBe("7 October 2026");15 expect(format(afternoon, "EEE, d MMM yy")).toBe("Wed, 7 Oct 26");16 expect(format(afternoon, "EEEE M/d")).toBe("Wednesday 10/7");17 });1819 test("shows the time on a 24-hour and a 12-hour clock", () => {20 expect(format(afternoon, "HH:mm:ss.SSS")).toBe("14:05:09.250");21 expect(format(afternoon, "h:mm a")).toBe("2:05 PM");22 expect(format(on(2026, 10, 7, 0, 5), "hh:mm a")).toBe("12:05 AM");23 expect(format(on(2026, 10, 7, 12), "h a")).toBe("12 PM");24 expect(format(on(2026, 10, 7, 9), "H")).toBe("9");25 });2627 test("shows the day of the month as an English ordinal", () => {28 const days = [1, 2, 3, 4, 10, 20, 21, 22, 23, 24, 30, 31];2930 expect(days.map((day) => format(on(2026, 1, day), "Do"))).toStrictEqual([31 "1st",32 "2nd",33 "3rd",34 "4th",35 "10th",36 "20th",37 "21st",38 "22nd",39 "23rd",40 "24th",41 "30th",42 "31st",43 ]);44 });4546 test("prints quoted text as it is, and '' as one quote", () => {47 expect(format(afternoon, "'Week of' MMMM d")).toBe("Week of October 7");48 expect(format(afternoon, "h 'o''clock'")).toBe("2 o'clock");49 expect(format(afternoon, "yyyy''MM")).toBe("2026'10");50 expect(format(afternoon, "'at' HH:mm 'sharp")).toBe("at 14:05 sharp");51 });5253 test("passes punctuation and spaces through", () => {54 expect(format(afternoon, "[yyyy] (MM) / dd.")).toBe("[2026] (10) / 07.");55 });5657 test("refuses a letter that is no token, an invalid date and an unknown zone", () => {58 expect(() => format(afternoon, "yyyy-MM-dd at HH")).toThrow(RangeError);59 expect(() => format(afternoon, "yyy")).toThrow(RangeError);60 expect(() => format(new Date(Number.NaN), "yyyy")).toThrow(RangeError);61 expect(() => format(afternoon, "HH", { timeZone: "Mars/Olympus_Mons" })).toThrow(RangeError);62 });6364 test("shows UTC's offset as Z", () => {65 expect(format(afternoon, "yyyy-MM-dd'T'HH:mm:ssXXX")).toBe("2026-10-07T14:05:09Z");66 });67});6869describe("format in a time zone", () => {70 test("shows the wall clock of the zone, with its offset", () => {71 const zoned = (timeZone: string) => format(afternoon, "yyyy-MM-dd HH:mm XXX EEE", { timeZone });7273 expect(zoned("Asia/Tokyo")).toBe("2026-10-07 23:05 +09:00 Wed");74 expect(zoned("Asia/Kolkata")).toBe("2026-10-07 19:35 +05:30 Wed");75 expect(zoned("America/Sao_Paulo")).toBe("2026-10-07 11:05 -03:00 Wed");76 expect(zoned("Pacific/Kiritimati")).toBe("2026-10-08 04:05 +14:00 Thu");77 expect(zoned("UTC")).toBe("2026-10-07 14:05 Z Wed");78 });7980 test("follows daylight saving time", () => {81 expect(format(on(2026, 7, 1, 12), "HH:mm XXX", { timeZone: "America/New_York" })).toBe(82 "08:00 -04:00",83 );84 expect(format(on(2026, 1, 15, 12), "HH:mm XXX", { timeZone: "America/New_York" })).toBe(85 "07:00 -05:00",86 );87 expect(offsetIn(on(2026, 7, 1), "Europe/Lisbon")).toBe(60);88 expect(offsetIn(on(2026, 12, 1), "Europe/Lisbon")).toBe(0);89 });9091 test("knows the zones the runtime knows", () => {92 expect(isTimeZone("Europe/Lisbon")).toBe(true);93 expect(isTimeZone("Mars/Olympus_Mons")).toBe(false);94 });95});