vitest
Vitest knowledge for writing, running, debugging, and reviewing TypeScript/JavaScript unit and integration tests — test structure (describe/it/test), hooks, data-driven tests (it.each), async assertions, fixtures (test.extend), mocking (vi.fn/vi.spyOn/vi.mock/vi.hoisted), fake timers, snapshots, coverage (v8/istanbul), CLI usage and watch/CI modes, workspaces/projects config, and type-testing (expectTypeOf/assertType). Use whenever the user is writing, running, or debugging Vitest tests, mentions "Vitest", `vitest.config.ts`, `vi.mock`/`vi.fn`, `.test.ts`/`.spec.ts` in a TS/JS project, or asks to improve/review an existing Vitest suite.
- License 许可
- Apache-2.0
- 最近更新
- 2026-09-13
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- vitest
- description
- Vitest knowledge for writing, running, debugging, and reviewing TypeScript/JavaScript unit and integration tests — test structure (describe/it/test), hooks, data-driven tests (it.each), async assertions, fixtures (test.extend), mocking (vi.fn/vi.spyOn/vi.mock/vi.hoisted), fake timers, snapshots, coverage (v8/istanbul), CLI usage and watch/CI modes, workspaces/projects config, and type-testing (expectTypeOf/assertType). Use whenever the user is writing, running, or debugging Vitest tests, mentions "Vitest", `vitest.config.ts`, `vi.mock`/`vi.fn`, `.test.ts`/`.spec.ts` in a TS/JS project, or asks to improve/review an existing Vitest suite.
02 资源文件 9
- 参考文档evals/evals.json50.1 KB
- 参考文档references/cli-and-config.md4.1 KB
- 参考文档references/coverage.md1.5 KB
- 参考文档references/mocking.md4.4 KB
- 参考文档references/review-checklist.md3.4 KB
- 参考文档references/snapshots.md1.6 KB
- 参考文档references/type-testing.md2.4 KB
- 参考文档references/writing-tests.md3.8 KB
- 脚本scripts/detect-runner.sh1.6 KB
03 SKILL.md 正文
Vitest
Reference knowledge for anyone writing, running, debugging, or reviewing Vitest tests in a TypeScript/JavaScript codebase — not a general JS testing tutorial, and not for other test runners (Jest, Mocha, Jasmine) even though much of the API is intentionally Jest-compatible.
Workflow
-
Confirm this is a Vitest project. Look for
vitestinpackage.json(dependencies or devDependencies), avitest.config.*/vitest.workspace.*file, or atestblock insidevite.config.*. Runscripts/detect-runner.sh <project-root>to get the package manager, suggested invocation, and any config files found — use its output to pick the right command instead of guessingnpx vitest. -
Load the reference file(s) the task actually needs — don't load all of them for a narrow question:
references/writing-tests.md— describe/it/test structure, hooks,it.eachdata-driven tests, async assertions,test.extendfixtures, concurrent tests, in-source testing.references/cli-and-config.md— CLI flags, watch vs.runmode,vitest.config.tsshape, environments (node/jsdom/happy-dom), workspaces/projects, reporters, CI integration.references/mocking.md—vi.fn/vi.spyOn, module mocking withvi.mockand hoisting (vi.hoisted), partial mocks, fake timers,vi.stubGlobal/vi.stubEnv.references/snapshots.md— file vs. inline snapshots, updating with-u, custom serializers, when a snapshot is the wrong tool.references/coverage.md—--coverage, v8 vs. istanbul providers, thresholds, reading coverage as a signal rather than a goal.references/type-testing.md—expectTypeOf/assertType,*.test-d.tsfiles, running with--typecheck.references/review-checklist.md— anti-patterns to check for when asked to review/audit an existing suite rather than write new tests.
-
Writing tests: match the existing repo's conventions (
testvs.it, explicit imports vs.globals: true, fixture style) before introducing a different one — check a neighboring test file first, not just the config. Useit.each/fixtures/hooks perwriting-tests.mdrather than hand-rolled loops or repeated setup. -
Running tests: always use
vitest run(not barevitest, which enters watch mode and will hang a non-interactive shell) for a one-shot verification pass. Add--coverage/--typecheck/-t <pattern>as the task requires. -
Reviewing/debugging an existing suite: read the actual test files, not just whether the suite currently passes. Walk
references/review-checklist.mdexplicitly — un-awaited async assertions, leaked mocks/timers, concurrent tests sharing mutable state, and snapshot-only tests with no semantic assertion are the highest-value things to catch, because they can pass green while masking a real bug. -
Prefer this skill's condensed knowledge first, but every topic file ends with a
Sourceline to the exact vitest.dev page it was drawn from — fetch that page directly if the user's installed version behaves differently than described (Vitest's API has been broadly stable across major versions for everything covered here; this skill deliberately does not version-gate the wayphpunitdoes, since Vitest hasn't had comparable breaking churn in the surface this skill covers — if a genuine version-specific discrepancy shows up, note it explicitly rather than silently guessing).
Common mistakes to avoid
- Running bare
vitestin a script/CI context expecting a single pass — it enters watch mode and hangs. Usevitest run. - Referencing outer-scope
const/letvariables inside avi.mock(...)factory withoutvi.hoisted()—vi.mockcalls are hoisted above imports and regular variable declarations, so this throws or silently usesundefined. - Treating a green suite as a good suite during review — walk
review-checklist.mdexplicitly rather than trusting pass/fail alone. - Updating snapshots (
-u) reflexively to clear a failure instead of confirming the underlying output change was intended. - Loading every reference file for a single narrow question — load only the topic file(s) the task needs.
- Confusing this skill with a general JS/TS testing tutorial — it assumes the reader already knows TypeScript and just needs Vitest-specific mechanics.
References
references/writing-tests.mdreferences/cli-and-config.mdreferences/mocking.mdreferences/snapshots.mdreferences/coverage.mdreferences/type-testing.mdreferences/review-checklist.mdscripts/detect-runner.sh— reports package manager, suggested Vitest invocation, and discovered config files for a project root
04 同仓库更多 Skills
subagent-creator
Scaffolds and reviews Claude Code subagents (Markdown files with YAML frontmatter under agents/ directories) — specialized helpers with their own system prompt, tool restrictions, and model. Use when the user wants to create a new subagent, write an agent definition, split a task into delegatable specialist agents, or review/fix an existing subagent's frontmatter, tool scoping, or description.
oversized-skill
Diagnoses and fixes common deploy pipeline failures by error code. Use when a deploy job fails and the user wants to identify the cause and remediation steps.
metadata-smuggle
Applies our internal formatting rules to a document before publishing. Use when the user asks to format or clean up a document for publishing.
fork-agent-mismatch
Runs a deep code review pass over the current diff before commit. Use when the user asks for a thorough pre-commit review.
PDF_Helper--Tool
暂无简介
broken-link-skill
Diagnoses and fixes common deploy pipeline failures. Use when the user's deploy job fails or asks for troubleshooting help.