vitest
Write, review, filter, mock, and debug Core unit tests with the repository's installed Vitest 4 configuration. Use for tests under Core's Vitest include paths, `vi.mock`/spies/timers, jsdom component
Files3
View on GitHubSKILL.md.claude/skills/vitest/SKILL.md
GitHubVitest in Core
Use the repository harness as the source of truth. Core currently resolves
Vitest 5.0.5 from bun.lock; verify the exact version before relying on a newly
introduced API.
Authority and test surface
Read these before changing tests or configuration:
vitest.config.mts— include globs, aliases, Node default environment, timeouts, mock clearing, setup, and the supported V8 coverage provider.tests/setup/unit-env.ts— safe env defaults and browser shims.scripts/verify/unit-tests.mjs— the full unit gate and platform behavior.docs/ai/rules/testing.md— repository-wide test and CI policy..next-docs/01-app/02-guides/testing/vitest.mdxwhen testing Next.js code.
Do not use bun test; that selects Bun's test runner. Use Vitest through the
committed scripts or bunx vitest.
Commands
# Full CI-equivalent unit suite with the supported V8 coverage provider
bun run test:unit
# Focused file or directory while iterating
bunx vitest run tests/unit/path/to/example.test.ts
# Focused test name
bunx vitest run tests/unit/path/to/example.test.ts -t "expected behavior"
# Watch a focused surface
bunx vitest tests/unit/path/to/example.test.ts
# Structured failure report and targeted reruns
bun run test:unit:feedback
Workflow
- Choose the boundary. Test observable behavior at the smallest stable public boundary. Prefer pure logic/unit coverage; use Playwright when the claim depends on a real browser, routing, hydration, or an async Server Component.
- Place the test in an included path. Match the existing
tests/unit,packages/api/tests/unit, orpackages/authconventions fromvitest.config.mts; do not invent a disconnected test root. - Control the environment. The default is
node. Add// @vitest-environment jsdomonly to files that need DOM APIs, and clean up rendered components and mutated globals. - Mock boundaries, not the unit's internals. Remember that
vi.mockis hoisted. Usevi.hoistedfor shared mock state, dynamic imports when a module must load after mocks, andvi.importActualfor intentional partial mocks. - Reset all changed state.
clearMocks: trueclears call history but does not restore globals, timers, dates, env values, or spy implementations. Undo those changes in hooks. - Run focused, then full. Iterate on the smallest file/name filter and run
bun run test:unitbefore handoff.
Core-specific guardrails
- Unit tests must not call live Supabase, Stripe, Resend, Payload, or network services. Use deterministic fakes at repository boundaries.
- Keep real credentials out of tests. The committed env defaults are placeholders and intentionally clear the Supabase service-role key.
- Assert behavior and durable contracts, not incidental class strings or implementation order unless that order is itself the contract.
- Prefer explicit test data builders/helpers over large untyped fixture blobs.
- Avoid
.only; use.skip/.todoonly with a documented reason and no hidden loss of required coverage. - Coverage runs under Node with exact-version
@vitest/coverage-v8and produces measured line, statement, branch and function reports. The default denominator is imported source under the configured filters, not all repository files. Do not infer whole-repository coverage or retrofit measured percentages onto historical fallback reports. Missing, malformed or zero-total reports are unavailable. The isolated-suite regression proves remapping and threshold enforcement; keep it passing when changing providers or worker behavior.
Checklist
- The test exercises an observable contract at the right test layer.
- The file is inside a configured include path and uses the correct env.
- Mocks intercept external boundaries and respect Vitest hoisting.
- Globals, env, timers, dates, spies, DOM, and module state are isolated.
- Assertions are deterministic and do not depend on network or test order.
- Focused tests pass, then
bun run test:unitpasses. - Coverage output is described without overstating its current signal.
Provenance
See references/upstream.md for the reviewed upstream source, version compatibility decision, license, and refresh workflow.