跳到正文
cccskills
免费在 GitHub 公开

tdesign-test-component

A skill for writing and maintaining unit tests for TDesign React components using Vitest, including testing patterns, best practices, and snapshot testing.

中文简介正在准备中。当前显示原文说明。

查看安装方法

包含的文件(1)

  • SKILL.md4.6 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

TDesign 组件测试指南

本技能提供 TDesign React 组件单元测试的最佳实践。

测试触发条件

当用户需要执行以下任务时使用此技能:

  • 为组件编写单元测试
  • 更新或修复测试用例
  • 运行快照测试
  • 提高测试覆盖率

测试框架和工具

工具用途
Vitest测试运行器
@testing-library/reactReact 组件测试工具
@testing-library/user-event用户交互模拟
@testing-library/jest-domDOM 断言扩展

@test/utils 已封装常用工具,包括:

  • render - 组件渲染
  • fireEvent - 事件触发
  • vi - Vitest mock 工具
  • act - React act wrapper
  • userEvent - 用户交互模拟
  • mockDelay / mockTimeout - 异步等待
  • simulateInputChange - 输入模拟
  • simulateKeydownEvent - 键盘事件模拟
  • waitFor - 异步断言

测试文件规范

文件位置和命名

packages/components/[component-name]/
└── __tests__/
    ├── [component-name].test.tsx    # 主组件单元测试(推荐)
    ├── [sub-module].test.tsx        # 子模块拆分测试(可选)
    └── __snapshots__/               # 快照目录(按需生成)

文件内容和结构

  • 固定顶层结构:每个组件的测试文件仅允许以下四种顶层 describe:props、events、instanceFunctions、scenarios。无相关测试内容时可省略对应分组。

  • 用例较少时:在对应的顶层 describe 中编写 test(...),不额外嵌套。

  • 用例较多时:在对应的顶层 describe 内,可按具体功能维度嵌套子 describe,再编写 test(...)。

  • 层级约束:禁止在组件根 describe 下直接编写 test(...),也禁止新增其他顶层 describe。

describe("ComponentName", () => {
  describe("props", () => {
    // 1. 按需使用 `propName[type]`(如 TNode、function、number)或 `propName[enumValue]`
    // 2. 方括号仅允许具体类型或合法枚举值,禁止写具体数值。数组类型统一写成 `array<T>`,禁止使用 `string[]` 等;
    //    布尔属性按常规方式开启(传入 `true` 或使用简写)时直接写属性名;仅显式验证关闭时写 `[false]`;同一用例覆盖两种取值时才写 `[boolean]`
    // 3. 同一属性的多个枚举值若测试结构与断言一致,应提取取值列表并使用 `forEach` 生成独立的 `test('propName[enumValue]')`,避免重复编写同构用例
    // 4. 同一行为涉及多个边界值或组合条件,且无需通过标题分别表达时,合并进单个 `test('propName')`,具体阈值与数据留在用例内
    // 5. 若标题必须补充复杂目的或场景描述时才能表达,应将用例移入 scenarios
  });
  describe("events", () => {
    // 回调 prop(onChange 等)是否触发
  });
  describe("instanceFunctions", () => {
    // ref 命令式 API、Plugin 静态方法等(无则省略)
  });
  describe("scenarios", () => {
    // 1. 覆盖多 API 联动的完整业务流;复杂时可嵌套子 `describe`
    // 2. 标题应明确涉及的 API 与测试目的,优先使用自然语言,仅在枚举值是关键条件时保留 `[enumValue]`
    // 3. 单 prop、多 prop 组合的简单渲染测试归入 `props`,不应放在此模块
    // 4. 仅验证回调是否触发及参数的测试归入 `events`
});

测试命令

pnpm test                              # 运行所有测试
pnpm test <component>                  # 运行指定组件测试
pnpm test -- --coverage                # 生成覆盖率报告
pnpm test -- --ui                      # 启动测试 UI
NODE_ENV=test-snap pnpm test           # 运行 CSR 快照测试
pnpm test -- --update                  # 更新所有快照
pnpm test <component> -- --update      # 更新指定组件快照

<component> 替换为实际组件名,例如 button。

快照测试文件:

  • CSR:test/snap/csr.test.jsx,自动为所有 _example/*.tsx 生成快照。
  • SSR:test/snap/ssr.test.jsx。

常见问题

Q: 如何处理异步组件?

A: 使用 mockDelay、mockTimeout、waitFor 或 act,在断言前等待 UI 更新。

Q: 快照测试失败怎么办?

A: 首先检查变更是否预期内。如果是预期变更,使用 --update 参数更新快照。

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

This skill should be used when developing or modifying styles for TDesign React components. It provides guidance on BEM naming conventions, CSS Variables usage, state classes, and the relationship between component code and common submodule styles.

中文简介正在准备中。当前显示原文说明。

Tencent/tdesign-react9712026年10月19日 更新

This skill should be used when creating a new TDesign React component. It provides a complete SOP for scaffolding components in the packages/components or packages/pro-components directories, including file structure, code patterns, and registration steps.

中文简介正在准备中。当前显示原文说明。

Tencent/tdesign-react9712026年10月19日 更新

This skill should be used when developing or modifying TDesign Pro Components (AIGC/Chat). It provides the ChatEngine architecture, component patterns, AG-UI protocol integration, and Generative UI development guide.

中文简介正在准备中。当前显示原文说明。

Tencent/tdesign-react9712026年10月19日 更新

查看 Tencent 的全部技能

报告此技能的问题