返回顶部
v

vitest-testingVitest测试实践

Vitest testing framework patterns and best practices. Use when writing unit tests, integration tests, configuring vitest.config, mocking with vi.mock/vi.fn, using snapshots, or setting up test coverage. Triggers on describe, it, expect, vi.mock, vi.fn, beforeEach, afterEach, vitest.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.1.0
安全检测
已通过
327
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

vitest-testing

Vitest 最佳实践

快速参考

ts
import { describe, it, expect, beforeEach, vi } from vitest

describe(功能名称, () => {
beforeEach(() => {
vi.clearAllMocks()
})

it(应执行特定操作, () => {
expect(actual).toBe(expected)
})

it.todo(计划中的测试)
it.skip(暂时禁用)
it.only(开发时仅运行此项)
})

常用断言

ts
// 相等性
expect(value).toBe(42) // 严格相等 (===)
expect(obj).toEqual({ a: 1 }) // 深度相等
expect(obj).toStrictEqual({ a: 1 }) // 严格深度相等(检查类型)

// 真值判断
expect(value).toBeTruthy()
expect(value).toBeFalsy()
expect(value).toBeNull()
expect(value).toBeUndefined()

// 数字
expect(0.1 + 0.2).toBeCloseTo(0.3)
expect(value).toBeGreaterThan(5)

// 字符串/数组
expect(str).toMatch(/pattern/)
expect(str).toContain(子字符串)
expect(array).toContain(元素)
expect(array).toHaveLength(3)

// 对象
expect(obj).toHaveProperty(key)
expect(obj).toHaveProperty(nested.key, value)
expect(obj).toMatchObject({ subset: 属性子集 })

// 异常
expect(() => fn()).toThrow()
expect(() => fn()).toThrow(错误消息)
expect(() => fn()).toThrow(/pattern/)

异步测试

ts
// Async/await(推荐)
it(获取数据, async () => {
const data = await fetchData()
expect(data).toEqual({ id: 1 })
})

// Promise 匹配器 - 务必使用 await
await expect(fetchData()).resolves.toEqual({ id: 1 })
await expect(fetchData()).rejects.toThrow(错误)

// 错误写法 - 会产生误报
expect(promise).resolves.toBe(value) // 缺少 await!

快速模拟参考

ts
const mockFn = vi.fn()
mockFn.mockReturnValue(42)
mockFn.mockResolvedValue({ data: value })

expect(mockFn).toHaveBeenCalled()
expect(mockFn).toHaveBeenCalledWith(arg1, arg2)
expect(mockFn).toHaveBeenCalledTimes(2)

附加文档

测试方法快速参考

方法用途
it() / test()定义测试
describe()
分组测试 | | beforeEach() / afterEach() | 每个测试的钩子 | | beforeAll() / afterAll() | 每个套件的钩子 | | .skip | 跳过测试/套件 | | .only | 仅运行此项 | | .todo | 占位符 | | .concurrent | 并行执行 | | .each([...]) | 参数化测试 |

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 vitest-testing-1775975348 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 vitest-testing-1775975348 技能

通过命令行安装

skillhub install vitest-testing-1775975348

下载

⬇ 下载 vitest-testing v1.1.0(免费)

文件大小: 10.04 KB | 发布时间: 2026-4-13 12:30

v1.1.0 最新 2026-4-13 12:30
- New best practices and usage reference for Vitest testing framework.
- Includes quick examples for unit/integration tests, assertions, mocking (vi.fn, vi.mock), async testing, and test metadata (skip, only, todo).
- Reference tables summarize common API methods and hooks.
- Links to additional documentation on mocking, configuration, and patterns.
- Emphasizes correct handling of async/promise tests to avoid false positives.

Archiver·手机版·闲社网·闲社论坛·智能体自动化市场· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2026 闲社网·AI智能体论坛·AI自动化解决方案·http://xianshe.com

p2p_official_large
返回顶部