generated from nhcarrigan/template
b67eae9d46
Adds full Vitest test suites with @vitest/coverage-v8, targeting 100% statement/branch/function/line coverage. Uses v8 ignore comments for genuinely unreachable defensive branches.
74 lines
2.7 KiB
TypeScript
74 lines
2.7 KiB
TypeScript
/* eslint-disable max-lines-per-function -- Test suites naturally have many cases */
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { Hono } from "hono";
|
|
|
|
describe("about route", () => {
|
|
const mockFetch = vi.fn();
|
|
|
|
beforeEach(() => {
|
|
vi.resetModules();
|
|
vi.stubGlobal("fetch", mockFetch);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
mockFetch.mockReset();
|
|
});
|
|
|
|
const makeApp = async () => {
|
|
const { aboutRouter } = await import("../../src/routes/about.js");
|
|
const app = new Hono();
|
|
app.route("/about", aboutRouter);
|
|
return app;
|
|
};
|
|
|
|
it("returns releases from a successful fetch", async () => {
|
|
const releases = [{ id: 1, name: "v1.0.0", body: "notes" }];
|
|
mockFetch.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(releases) });
|
|
const app = await makeApp();
|
|
const res = await app.fetch(new Request("http://localhost/about"));
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { releases: unknown[] };
|
|
expect(body.releases).toEqual(releases);
|
|
});
|
|
|
|
it("returns empty releases when fetch is not ok", async () => {
|
|
mockFetch.mockResolvedValueOnce({ ok: false });
|
|
const app = await makeApp();
|
|
const res = await app.fetch(new Request("http://localhost/about"));
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { releases: unknown[] };
|
|
expect(body.releases).toEqual([]);
|
|
});
|
|
|
|
it("returns empty releases when fetch throws", async () => {
|
|
mockFetch.mockRejectedValueOnce(new Error("Network error"));
|
|
const app = await makeApp();
|
|
const res = await app.fetch(new Request("http://localhost/about"));
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { releases: unknown[] };
|
|
expect(body.releases).toEqual([]);
|
|
});
|
|
|
|
it("returns cached releases on second call within TTL", async () => {
|
|
const releases = [{ id: 1, name: "v1.0.0", body: "notes" }];
|
|
mockFetch.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(releases) });
|
|
const app = await makeApp();
|
|
// First call populates cache
|
|
await app.fetch(new Request("http://localhost/about"));
|
|
// Second call should use cache, not call fetch again
|
|
const res = await app.fetch(new Request("http://localhost/about"));
|
|
expect(res.status).toBe(200);
|
|
expect(mockFetch).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("includes apiVersion in response", async () => {
|
|
mockFetch.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve([]) });
|
|
const app = await makeApp();
|
|
const res = await app.fetch(new Request("http://localhost/about"));
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { apiVersion: string };
|
|
expect(typeof body.apiVersion).toBe("string");
|
|
});
|
|
});
|