Create test

From wot-ui/wot-ui · View interactive version

Create test is an AI agent skill from the GitHub repository wot-ui/wot-ui. 为组件创建单元测试的标准流程

How to use this skill

Skill file

创建测试技能

本技能用于为 wot-ui 项目中的组件创建单元测试。测试框架使用 vitest 和 @vue/test-utils。

目录结构

所有组件测试位于 tests/components/ 目录下,命名格式为 wd-{组件名}.test.ts。

创建步骤

1. 确认组件信息

2. 创建测试文件

在 tests/components/ 目录下创建 wd-{组件名}.test.ts 文件。

3. 使用测试模板

将以下模板复制到测试文件中,并根据组件实际情况进行修改。

import { mount } from '@vue/test-utils'
import Wd{组件名帕斯卡} from '@/uni_modules/wot-ui/components/wd-{组件名}/wd-{组件名}.vue'
import { describe, test, expect, vi, beforeEach } from 'vitest'

describe('Wd{组件名帕斯卡}', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  // 1. 基本渲染测试
  test('基本渲染', () => {
    const wrapper = mount(Wd{组件名帕斯卡})
    // 检查根类名
    expect(wrapper.classes()).toContain('wd-{组件名}')
    // 检查是否有 snapshot (可选)
    // expect(wrapper.element).toMatchSnapshot()
  })

  // 2. Props 测试
  // 遍历 types.ts 中定义的所有 props,测试其效果
  test('Props: propName', () => {
    const wrapper = mount(Wd{组件名帕斯卡}, {
      props: {
        propName: 'value'
      }
    })
    // 断言 props 生效,例如检查样式、类名或 DOM 结构
    expect(wrapper.classes()).toContain('is-some-state')
  })

  // 3. Events 测试
  test('Events: click', async () => {
    const wrapper = mount(Wd{组件名帕斯卡})
    await wrapper.trigger('click')
    expect(wrapper.emitted('click')).toBeTruthy()
  })

  // 4. Slots 测试
  test('Slots: default', () => {
    const content = 'Slot Content'
    const wrapper = mount(Wd{组件名帕斯卡}, {
      slots: {
        default: content
      }
    })
    expect(wrapper.text()).toContain(content)
  })
})

4. 编写具体测试用例

根据 types.ts 中的 Props 定义和组件逻辑,完善测试用例。

5. 运行测试

使用以下命令运行测试,确保通过:

# 运行所有测试
npm run test

# 运行特定文件测试 (推荐)
npx vitest tests/components/wd-{组件名}.test.ts

6. 提升覆盖率技巧

为了确保高测试覆盖率,请关注以下方面:

最佳实践

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.