Skip to content

第二十三章:测试策略

分层测试、覆盖率目标、Storybook + Vitest 集成


23.1 测试分层

层级工具测试内容运行频率
单元测试Vitest (jsdom)业务逻辑、工具函数、Hooks每次提交
组件测试Storybook + Vitest (浏览器)组件渲染、交互每次提交
E2E 测试Playwright完整用户流程PR / 发布前

23.2 测试命令

bash
pnpm test              # 单元测试 (*.test.ts)
pnpm test:storybook    # Storybook 组件测试 (*.stories.tsx)
pnpm test:all          # 运行所有测试
pnpm test:coverage     # 单元测试 + 覆盖率报告

23.3 Storybook + Vitest 集成

项目使用 @storybook/addon-vitest 将 Stories 作为测试用例运行。

测试类型对比

文件类型测试内容运行环境价值
*.test.ts纯逻辑/函数jsdom (快)验证业务逻辑正确性
*.stories.tsx (无 play)组件能否渲染真实浏览器冒烟测试,防止渲染崩溃
*.stories.tsx (有 play)用户交互流程真实浏览器集成测试

配置文件

vitest.config.ts          # 定义 unit 和 storybook 两个项目
.storybook/vitest.setup.ts # Storybook 测试初始化
.storybook/preview.tsx     # 全局 decorators (i18n, QueryClient, theme)

Story 编写规范

基础 Story(自动冒烟测试):

tsx
export const Default: Story = {
  args: { value: 'test' },
}

带交互测试的 Story:

tsx
import { within, userEvent, expect } from 'storybook/test'

export const Interactive: Story = {
  play: async ({ canvasElement }) => {
    const canvas = within(canvasElement)
    const input = canvas.getByRole('textbox')
    await userEvent.type(input, 'hello')
    expect(canvas.getByText('hello')).toBeInTheDocument()
  },
}

全局 Decorators

.storybook/preview.tsx 中配置全局 Provider:

tsx
// 必须包装的 Provider
- I18nextProvider      # 国际化
- QueryClientProvider  # TanStack Query
- ThemeProvider        # 主题切换

何时使用 play 函数

场景是否需要 play
纯展示组件否 - 自动冒烟测试足够
复杂交互(手势、拖拽)
表单验证流程
状态机组件
已有 *.test.ts 覆盖的逻辑否 - 避免重复

23.4 覆盖率目标

类型目标
语句覆盖≥ 70%
分支覆盖≥ 70%
函数覆盖≥ 70%
行覆盖≥ 70%

23.5 测试优先级

优先级测试内容
P0密钥生成/派生、加密/解密、签名验证
P1钱包创建/导入、转账流程
P2余额查询、交易历史
P3设置、语言切换

23.6 测试命名规范

typescript
describe('WalletStore', () => {
  describe('addWallet', () => {
    it('should add wallet to list', () => {})
    it('should set as current wallet', () => {})
    it('should persist to localStorage', () => {})
  })
})

23.7 CI 集成

CI 流水线运行以下测试:

yaml
# .github/workflows/ci.yml
pnpm turbo run test:run test:storybook e2e:ci
步骤说明
test:run单元测试 (jsdom)
test:storybookStorybook 组件测试 (chromium)
e2e:ciE2E 集成测试

本章小结

  • 三层测试:单元 → 组件 → E2E
  • Storybook Stories 自动作为冒烟测试运行
  • 复杂交互使用 play 函数
  • 避免 *.test.tsplay 函数测试重复
  • 覆盖率目标 70%
  • 安全相关代码优先测试

Released under the MIT License.