Skip to content

feat: 添加测试基础设施并修复工具函数 bug#13

Merged
Rinisnotarobot merged 1 commit into
masterfrom
test/board-system-validation
May 29, 2026
Merged

feat: 添加测试基础设施并修复工具函数 bug#13
Rinisnotarobot merged 1 commit into
masterfrom
test/board-system-validation

Conversation

@Rinisnotarobot

Copy link
Copy Markdown
Owner

Summary

  • 添加 Vitest 单元测试配置和 Playwright E2E 测试基础设施
  • 为 hooks、utils、validators 添加全面的单元测试覆盖
  • 修复 slug.ts 连字符未合并问题
  • 修复 time.ts 未来日期返回语义误导的问题

Bug 修复详情

slug.ts

  • 问题: generateSlug('test---article') 返回 'test---article',产生不良 URL
  • 修复: 添加 .replace(/-+/g, '-') 合并连续连字符

time.ts

  • 问题: 未来日期返回 "刚刚" 语义误导(如定时发布的文章显示为"刚刚发布")
  • 修复: 为未来日期返回正确的相对时间("明天"、"3天后"、"2周后"等)

Test Plan

  • pnpm test 单元测试通过 (160 tests)
  • pnpm tsc --noEmit 类型检查通过
  • pnpm test:e2e E2E 测试(需本地验证)
  • CI 通过

🤖 Generated with Claude Code

测试基础设施:
- Vitest 单元测试配置 + coverage-v8
- Playwright E2E 测试配置
- 测试工具和 fixtures (auth helpers)
- Prisma seed 脚本用于 E2E 测试数据

单元测试覆盖:
- hooks: useDebounce, useIsBreakpoint, useMobile
- utils: slug, time, tag
- validators: auth, article, bookmark, comment, follow, like, profile, tag

Bug 修复:
- slug.ts: 合并连续连字符,避免生成不良 URL
- time.ts: 未来日期返回语义正确的相对时间 (明天/X天后) 而非误导性的 "刚刚"

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 29, 2026 04:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 为项目引入了 Vitest 单测与 Playwright E2E 测试基础设施,并补齐了一批 hooks/utils/validators 的单元测试,同时修复了 slug 连字符合并与 timeAgo 未来时间语义问题。

Changes:

  • 新增 Vitest 配置、测试 setup 与 React Testing Library 工具封装,补齐 validators/hooks/utils 的单测覆盖
  • 新增 Playwright 配置与基础 E2E smoke tests、认证相关 helpers/fixtures,并加入 Prisma seed 用于 E2E 数据
  • 修复 generateSlug 连续连字符未合并问题;调整 timeAgo 未来日期的相对时间语义

Reviewed changes

Copilot reviewed 26 out of 28 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
vitest.config.ts 增加 Vitest 配置(环境、覆盖率阈值、别名、插件)以支持单测运行
src/test/setup.ts Vitest 全局 setup:jest-dom、cleanup、mock server-only 与 stub env
src/test/utils/test-utils.tsx 封装 RTL render,注入 React Query Provider 便于 hooks/组件测试
playwright.config.ts 新增 Playwright E2E 配置(projects/webServer/baseURL 等)
tests/e2e/auth.spec.ts 新增基础 smoke/E2E 路由可达性与重定向断言
tests/e2e/helpers/auth.ts 新增 E2E 登录/登出/注册 helper 与测试账号常量
tests/e2e/fixtures/auth.ts 新增 Playwright fixture:提供已登录页面(用户/作者)
prisma/seed.ts 新增 Prisma seed:创建 E2E 测试用户、标签、文章、评论等数据
package.json 增加 prisma seed 配置;引入 Playwright、RTL、coverage provider 等 devDeps
pnpm-lock.yaml 锁文件更新以引入新增测试依赖
.gitignore 忽略 coverage 与 test-results 等测试产物目录
src/lib/utils/slug.ts generateSlug 追加合并连续连字符逻辑
src/lib/utils/slug.test.ts 新增 generateSlug 单测覆盖(含连续连字符 case)
src/lib/utils/time.ts timeAgo 增加未来日期输出(“明天/3 天后/2 周后…”)
src/lib/utils/time.test.ts 新增 timeAgo 单测覆盖(含未来日期)
src/lib/utils/tag.test.ts 新增 searchTags 工具函数单测覆盖
src/hooks/use-mobile.test.ts 新增 useIsMobile hook 单测覆盖
src/hooks/use-is-breakpoint.test.ts 新增 useIsBreakpoint hook 单测覆盖
src/hooks/use-debounce.test.ts 新增 useDebounce hook 单测覆盖
src/lib/validators/common.test.ts 新增通用校验器单测
src/lib/validators/auth.test.ts 新增 auth 相关 schema 单测
src/lib/validators/article.test.ts 新增 article 相关 schema 单测
src/lib/validators/bookmark.test.ts 新增 bookmark 相关 schema 单测
src/lib/validators/comment.test.ts 新增 comment 相关 schema 单测
src/lib/validators/follow.test.ts 新增 follow 相关 schema 单测
src/lib/validators/like.test.ts 新增 like 相关 schema 单测
src/lib/validators/profile.test.ts 新增 profile 相关 schema 单测
src/lib/validators/tag.test.ts 新增 tag 相关 schema 单测
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/lib/utils/time.ts
Comment on lines 9 to +16
const diffMs = now.getTime() - then.getTime()
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24))

// Handle future dates with proper semantics
if (diffDays < 0) {
const futureDays = Math.abs(diffDays)
if (futureDays === 0) return "今天"
if (futureDays === 1) return "明天"
Comment thread vitest.config.ts
Comment on lines +1 to +4
import { defineConfig } from 'vitest/config'
import { reactCompilerPreset } from '@vitejs/plugin-react'
import babel from '@rolldown/plugin-babel'
import path from 'path'
Comment thread vitest.config.ts
Comment on lines +31 to +35
resolve: {
alias: {
'#': path.resolve(__dirname, './src'),
'@': path.resolve(__dirname, './src'),
},
Comment on lines +47 to +52
await page.goto('/sign-up')
await page.getByLabel(/姓名|name/i).fill(user.name)
await page.locator('#email').fill(user.email)
await page.locator('#password').fill(user.password)
await page.getByRole('button', { name: '注册' }).click()
await page.waitForURL('/', { timeout: 10000 })
Comment thread tests/e2e/helpers/auth.ts
Comment on lines +57 to +67
await page.goto('/sign-up')

await page.getByPlaceholder(/姓名|name/i).fill(userData.name)
await page.getByPlaceholder(/邮箱|email/i).fill(userData.email)
await page.getByPlaceholder(/密码|password/i).fill(userData.password)

// Submit
await page.getByRole('button', { name: /注册|signup/i }).click()

// Wait for redirect or verification page
await page.waitForURL(/\/|verify/, { timeout: 10000 })
Comment thread package.json
"@types/react": "^19.2.0",
"@types/react-dom": "^19.2.0",
"@vitejs/plugin-react": "^6.0.1",
"@vitest/coverage-v8": "^4.1.7",
@Rinisnotarobot Rinisnotarobot merged commit 1a8c258 into master May 29, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants