From b98c302701f1dae3aa0b40abfe27eb22158fa495 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 9 Jun 2026 22:11:37 +0000 Subject: [PATCH] test: add unit test suite and PR workflow Co-authored-by: Jet Semrick --- .github/workflows/test.yml | 27 +++++++++++++++++++++++++ package.json | 3 ++- src/lib/posts.test.ts | 41 ++++++++++++++++++++++++++++++++++++++ src/lib/utils.test.ts | 21 +++++++++++++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/test.yml create mode 100644 src/lib/posts.test.ts create mode 100644 src/lib/utils.test.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..3471854 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,27 @@ +name: Tests + +on: + pull_request: + +jobs: + unit-tests: + name: Unit tests + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Bun + uses: oven-sh/setup-bun@v2 + with: + bun-version: latest + + - name: Install dependencies + run: bun install --frozen-lockfile + + - name: Lint + run: bun run lint + + - name: Test + run: bun run test diff --git a/package.json b/package.json index 380ec03..97491ca 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "dev": "next dev", "build": "next build", "start": "next start", - "lint": "eslint" + "lint": "eslint", + "test": "bun test" }, "dependencies": { "@tailwindcss/typography": "^0.5.19", diff --git a/src/lib/posts.test.ts b/src/lib/posts.test.ts new file mode 100644 index 0000000..9828037 --- /dev/null +++ b/src/lib/posts.test.ts @@ -0,0 +1,41 @@ +import { describe, expect, test } from "bun:test"; + +import { getAllPostIds, getPostData, getSortedPostsData } from "./posts"; + +describe("getSortedPostsData", () => { + test("reads post frontmatter sorted by newest date first", () => { + expect(getSortedPostsData()).toEqual([ + { + id: "hello-world", + title: "Hello, world", + date: "2024-01-01", + description: "A short introduction.", + }, + ]); + }); +}); + +describe("getAllPostIds", () => { + test("returns route params for markdown posts", () => { + expect(getAllPostIds()).toEqual([ + { + params: { + slug: "hello-world", + }, + }, + ]); + }); +}); + +describe("getPostData", () => { + test("returns frontmatter and rendered markdown content", async () => { + await expect(getPostData("hello-world")).resolves.toMatchObject({ + id: "hello-world", + title: "Hello, world", + date: "2024-01-01", + description: "A short introduction.", + contentHtml: + "

Welcome. This is the first entry \u2014 more soon on engineering, debate, and building Cursor.

\n", + }); + }); +}); diff --git a/src/lib/utils.test.ts b/src/lib/utils.test.ts new file mode 100644 index 0000000..c109fbf --- /dev/null +++ b/src/lib/utils.test.ts @@ -0,0 +1,21 @@ +import { describe, expect, test } from "bun:test"; + +import { cn, formatDate } from "./utils"; + +describe("cn", () => { + test("merges conditional classes", () => { + expect(cn("base", false && "hidden", ["flex", "items-center"])).toBe( + "base flex items-center", + ); + }); + + test("resolves conflicting Tailwind classes", () => { + expect(cn("px-2 py-1", "px-4")).toBe("py-1 px-4"); + }); +}); + +describe("formatDate", () => { + test("formats dates in UTC", () => { + expect(formatDate("2024-01-01")).toBe("January 1, 2024"); + }); +});