Skip to content
37 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { exactTime, timeAgo } from "./time-ago.ts";
5
6const NOW = Date.parse("2026-10-13T12:00:00.000Z");
7const before = (ms: number) => new Date(NOW - ms).toISOString();
8const MINUTE = 60_000;
9const HOUR = 60 * MINUTE;
10const DAY = 24 * HOUR;
11
12test("a moment within the last week is said in minutes, hours or days", () => {
13 assert.equal(timeAgo(before(10_000), NOW, "UTC"), "just now");
14 assert.equal(timeAgo(before(-30_000), NOW, "UTC"), "just now");
15 assert.equal(timeAgo(before(50_000), NOW, "UTC"), "1m ago");
16 assert.equal(timeAgo(before(5 * MINUTE), NOW, "UTC"), "5m ago");
17 assert.equal(timeAgo(before(3 * HOUR), NOW, "UTC"), "3h ago");
18 assert.equal(timeAgo(before(30 * HOUR), NOW, "UTC"), "yesterday");
19 assert.equal(timeAgo(before(4 * DAY), NOW, "UTC"), "4d ago");
20});
21
22test("a moment weeks ago is said in weeks, and older ones by their date", () => {
23 assert.equal(timeAgo(before(10 * DAY), NOW, "UTC"), "1w ago");
24 assert.equal(timeAgo(before(27 * DAY), NOW, "UTC"), "3w ago");
25 assert.equal(timeAgo("2026-09-01T09:00:00.000Z", NOW, "UTC"), "Sep 1");
26 assert.equal(timeAgo("2023-07-24T09:00:00.000Z", NOW, "UTC"), "Jul 24, 2023");
27 assert.equal(timeAgo(Date.parse("2023-07-24T09:00:00.000Z"), NOW, "UTC"), "Jul 24, 2023");
28});
29
30test("a moment that can't be read says nothing", () => {
31 assert.equal(timeAgo("not a date", NOW), "");
32 assert.equal(exactTime("not a date"), "");
33});
34
35test("the hint gives the moment in full", () => {
36 assert.equal(exactTime("2026-10-13T15:04:00.000Z", "UTC"), "Oct 13, 2026, 3:04 PM UTC");
37});