返回顶部
v

vitest-testing

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
安全检测
已通过
305
下载量
0
收藏
概述
安装方式
版本历史

vitest-testing

# Vitest Best Practices ## Quick Reference ```ts import { describe, it, expect, beforeEach, vi } from 'vitest' describe('feature name', () => { beforeEach(() => { vi.clearAllMocks() }) it('should do something specific', () => { expect(actual).toBe(expected) }) it.todo('planned test') it.skip('temporarily disabled') it.only('run only this during dev') }) ``` ## Common Assertions ```ts // Equality expect(value).toBe(42) // Strict (===) expect(obj).toEqual({ a: 1 }) // Deep equality expect(obj).toStrictEqual({ a: 1 }) // Strict deep (checks types) // Truthiness expect(value).toBeTruthy() expect(value).toBeFalsy() expect(value).toBeNull() expect(value).toBeUndefined() // Numbers expect(0.1 + 0.2).toBeCloseTo(0.3) expect(value).toBeGreaterThan(5) // Strings/Arrays expect(str).toMatch(/pattern/) expect(str).toContain('substring') expect(array).toContain(item) expect(array).toHaveLength(3) // Objects expect(obj).toHaveProperty('key') expect(obj).toHaveProperty('nested.key', 'value') expect(obj).toMatchObject({ subset: 'of properties' }) // Exceptions expect(() => fn()).toThrow() expect(() => fn()).toThrow('error message') expect(() => fn()).toThrow(/pattern/) ``` ## Async Testing ```ts // Async/await (preferred) it('fetches data', async () => { const data = await fetchData() expect(data).toEqual({ id: 1 }) }) // Promise matchers - ALWAYS await these await expect(fetchData()).resolves.toEqual({ id: 1 }) await expect(fetchData()).rejects.toThrow('Error') // Wrong - creates false positive expect(promise).resolves.toBe(value) // Missing await! ``` ## Quick Mock Reference ```ts const mockFn = vi.fn() mockFn.mockReturnValue(42) mockFn.mockResolvedValue({ data: 'value' }) expect(mockFn).toHaveBeenCalled() expect(mockFn).toHaveBeenCalledWith('arg1', 'arg2') expect(mockFn).toHaveBeenCalledTimes(2) ``` ## Additional Documentation - **Mocking**: See [references/mocking.md](references/mocking.md) for module mocking, spying, cleanup - **Configuration**: See [references/config.md](references/config.md) for vitest.config, setup files, coverage - **Patterns**: See [references/patterns.md](references/patterns.md) for timers, snapshots, anti-patterns ## Test Methods Quick Reference | Method | Purpose | |--------|---------| | `it()` / `test()` | Define test | | `describe()` | Group tests | | `beforeEach()` / `afterEach()` | Per-test hooks | | `beforeAll()` / `afterAll()` | Per-suite hooks | | `.skip` | Skip test/suite | | `.only` | Run only this | | `.todo` | Placeholder | | `.concurrent` | Parallel execution | | `.each([...])` | Parameterized tests |

标签

skill ai

通过对话安装

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

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

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

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

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

通过命令行安装

skillhub install vitest-testing-1775975348

下载 Zip 包

⬇ 下载 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-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部