unit-testing-vitest
Skill0 starsChanged 4 months agoMITView on GitHub
Unit testing guidelines for Vitest (and Jest-compatible) JavaScript/TypeScript projects. Use this skill whenever writing, reviewing, or refactoring unit tests, adding vi mocks, configuring coverage, or debugging flaky/async test failures. Triggers on tasks involving *.test.ts, *.spec.ts files, vi.fn/vi.mock/vi.spyOn usage, Vitest config, or any request to "write tests", "add unit tests", "mock this", or "improve test coverage".
In this repository6 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- api-testingSkill
- e2e-testing-playwrightSkill
- integration-testingSkill
- performance-testingSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 70
Read rules/_sections.md for full category descriptions.
- Distinctive: line 72
When reviewing existing tests, check them against the HIGH-impact rules first (structure, assertions, mocking, async), then MEDIUM.
- Very specific: line 73
When writing new tests, default to the AAA pattern, prefer vi.spyOn over full vi.mock when only one function needs stubbing, and always …
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Unit Testing (Vitest)
Guidelines for writing reliable, fast, and maintainable unit tests with Vitest in JS/TS projects. Contains 13 rules across 6 categories, prioritized by impact.
When to Apply
Reference these guidelines when:
- Writing new unit tests for functions, classes, or components
- Adding or reviewing
vi.fn()/vi.mock()/vi.spyOn()usage - Configuring Vitest coverage thresholds or CI gates
- Debugging a test that's flaky, hangs, or silently passes
- Refactoring code and needing tests that won't break on implementation changes
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
| 1 | Test Structure & Naming | HIGH | structure- |
| 2 | Assertions | HIGH | assert- |
| 3 | Mocking & Test Doubles | HIGH | mock- |
| 4 | Async & Timers | HIGH | async- |
| 5 | Coverage & CI | MEDIUM | coverage- |
| 6 | Performance & Isolation | MEDIUM | perf- |
Quick Reference
1. Test Structure & Naming (HIGH)
structure-aaa-pattern- Arrange-Act-Assert in every teststructure-descriptive-names- Name tests after behavior, not methodstructure-one-behavior-per-test- One assertion focus per test
2. Assertions (HIGH)
assert-specific-matchers- Use the most specific matcher availableassert-behavior-not-implementation- Assert output, not internals
3. Mocking & Test Doubles (HIGH)
mock-vi-fn-spyon- Choose vi.fn/vi.spyOn/vi.mock deliberatelymock-reset-restore- Reset/restore mocks between testsmock-avoid-mocking-what-you-own- Don't mock code you can test directly
4. Async & Timers (HIGH)
async-await-promises- Always await async assertionsasync-fake-timers- Use vi.useFakeTimers() instead of real delays
5. Coverage & CI (MEDIUM)
coverage-ci-thresholds- Gate CI on explicit coverage thresholdscoverage-dont-chase-100- Don't chase 100% on low-risk code
6. Performance & Isolation (MEDIUM)
perf-test-isolation- Keep tests isolated for safe parallel runs
How to Use This Skill
- Read
rules/_sections.mdfor full category descriptions. - For a specific task, open the relevant rule file(s) under
rules/— each has an incorrect/correct code example. - When reviewing existing tests, check them against the HIGH-impact rules first (structure, assertions, mocking, async), then MEDIUM.
- When writing new tests, default to the AAA pattern, prefer
vi.spyOnover fullvi.mockwhen only one function needs stubbing, and alwaysawaitasync assertions.
See rules/_template.md for the format used by every rule file.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

