1
0

test: 重构测试体系 — 建立组件测试层、补充后端测试、清理低质量测试

- 新增 jsdom + @testing-library/react 组件测试环境
- 新增 12 个组件测试,覆盖所有前端组件
- 补充后端 middleware 和 helpers 单元测试
- 删除伪测试 use-target-detail-logic.test.ts
- 精简过度枚举的 color-threshold.test.ts
- 新增 bunfig.toml 配置测试 preload
- 更新 DEVELOPMENT.md 测试章节
- 安装 @types/jsdom 修复类型声明
This commit is contained in:
2026-05-15 18:31:33 +08:00
parent 2b08f81a0d
commit 8793fbd786
24 changed files with 1392 additions and 143 deletions

View File

@@ -0,0 +1,109 @@
import { describe, expect, test } from "bun:test";
import { createApiError, createHeaders, formatDuration, jsonResponse } from "../../src/server/helpers";
describe("createApiError", () => {
test("创建错误响应对象", () => {
const result = createApiError("Not found", 404);
expect(result).toEqual({ error: "Not found", status: 404 });
});
test("支持不同的错误消息和状态码", () => {
const badRequest = createApiError("Bad request", 400);
const internalError = createApiError("Internal error", 500);
expect(badRequest).toEqual({ error: "Bad request", status: 400 });
expect(internalError).toEqual({ error: "Internal error", status: 500 });
});
});
describe("createHeaders", () => {
test("生产模式添加安全 headers", () => {
const headers = createHeaders("production", { "Content-Type": "application/json" });
expect(headers.get("X-Content-Type-Options")).toBe("nosniff");
expect(headers.get("Referrer-Policy")).toBe("strict-origin-when-cross-origin");
expect(headers.get("Content-Type")).toBe("application/json");
});
test("非生产模式不添加安全 headers", () => {
const headers = createHeaders("test", { "Content-Type": "application/json" });
expect(headers.get("X-Content-Type-Options")).toBeNull();
expect(headers.get("Referrer-Policy")).toBeNull();
expect(headers.get("Content-Type")).toBe("application/json");
});
test("保留传入的自定义 headers", () => {
const headers = createHeaders("production", { "X-Custom-Header": "custom-value" });
expect(headers.get("X-Custom-Header")).toBe("custom-value");
});
});
describe("jsonResponse", () => {
test("创建 JSON 响应", () => {
const body = { message: "Hello" };
const response = jsonResponse(body, { mode: "test" });
expect(response.status).toBe(200);
expect(response.headers.get("Content-Type")).toBe("application/json; charset=utf-8");
});
test("生产模式响应包含安全 headers", () => {
const response = jsonResponse({ data: "test" }, { mode: "production" });
expect(response.headers.get("X-Content-Type-Options")).toBe("nosniff");
expect(response.headers.get("Referrer-Policy")).toBe("strict-origin-when-cross-origin");
});
test("支持自定义状态码", () => {
const response = jsonResponse({ error: "Not found" }, { mode: "test", status: 404 });
expect(response.status).toBe(404);
});
test("支持自定义 headers", () => {
const response = jsonResponse(
{ data: "test" },
{
headers: { "X-Custom": "value" },
mode: "test",
},
);
expect(response.headers.get("X-Custom")).toBe("value");
});
test("响应 body 可以被解析为 JSON", async () => {
const body = { count: 42, message: "Hello" };
const response = jsonResponse(body, { mode: "test" });
const parsed = (await response.json()) as { count: number; message: string };
expect(parsed).toEqual(body);
});
});
describe("formatDuration", () => {
test("毫秒格式化", () => {
expect(formatDuration(100)).toBe("100ms");
expect(formatDuration(999)).toBe("999ms");
});
test("秒格式化(整秒)", () => {
expect(formatDuration(1000)).toBe("1s");
expect(formatDuration(5000)).toBe("5s");
expect(formatDuration(59000)).toBe("59s");
});
test("分钟格式化(整分钟)", () => {
expect(formatDuration(60000)).toBe("1m");
expect(formatDuration(120000)).toBe("2m");
expect(formatDuration(300000)).toBe("5m");
});
test("非整秒/整分钟保持毫秒", () => {
expect(formatDuration(1500)).toBe("1500ms");
expect(formatDuration(61123)).toBe("61123ms");
});
});