feat: 添加测试基础设施并修复工具函数 bug#13
Merged
Merged
Conversation
测试基础设施: - 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>
There was a problem hiding this comment.
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 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 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 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 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 }) |
| "@types/react": "^19.2.0", | ||
| "@types/react-dom": "^19.2.0", | ||
| "@vitejs/plugin-react": "^6.0.1", | ||
| "@vitest/coverage-v8": "^4.1.7", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Bug 修复详情
slug.ts
generateSlug('test---article')返回'test---article',产生不良 URL.replace(/-+/g, '-')合并连续连字符time.ts
Test Plan
pnpm test单元测试通过 (160 tests)pnpm tsc --noEmit类型检查通过pnpm test:e2eE2E 测试(需本地验证)🤖 Generated with Claude Code