vitest

A set of guidelines for Vitest, a JavaScript and TypeScript tool used to test code. It covers unit tests, mocks, code coverage, and snapshot tests.

In plain words
What is it for?
Use it when writing or reviewing Vitest tests, including test cases, error handling, mocked functions, and test configuration.
Why use it?
It provides consistent ways to structure tests, check results, and isolate code from its dependencies. This makes tests easier to read and maintain.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/nedcodes-ok/cursor-doctor/vitest
Clone the repo
git clone --depth 1 https://github.com/nedcodes-ok/cursor-doctor
Per session 647 This file is loaded in full into every session.
When invoked 647 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00647 $0.00647
Opus 5 $0.00324 $0.00324
Sonnet 5 $0.00129 $0.00129
Haiku 4.5 $0.00065 $0.00065

Measured 2d ago against content hash 8fb1b16596a3, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

vitest scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

pro-kit/templates/tools/vitest.mdc · 57 lines

How it starts

The opening of the file, as written. The whole thing — 57 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Vitest Cursor Rules

You are an expert in Vitest testing. Follow these rules:

Test Structure

  • Colocate test files with source: utils.ts → utils.test.ts (same directory)
  • Use describe blocks to group related tests by function or behavior
  • Test names describe expected behavior: it('returns empty array when no items match filter')
  • Follow AAA pattern: Arrange, Act, Assert — separated by blank lines
  • One assertion per test when possible, multiple only for closely related checks

Assertions

  • Use expect().toBe() for primitives, expect().toEqual() for objects/arrays
  • Use expect().toStrictEqual() when you care about undefined properties and class instances
  • Use expect().toMatchObject() for partial object matching
  • Use expect().toThrowError('message') to test error cases — always check the message
  • Prefer specific matchers: .toContain(), .toHaveLength(), .toHaveProperty() over generic .toBe(true)

Mocking

  • Use vi.mock('module') at the top of the file for module mocks
  • Use vi.fn() for function mocks, vi.spyOn() when you need the original implementation
  • Reset mocks in beforeEach or use { clearMocks: true } in vitest config
  • Type mocks properly: vi.mocked(myFunction) to get typed mock instance
  • Mock at the boundary (API calls, file system), not internal functions

Async Testing

  • Use async/await in test functions — no done callbacks
  • Use vi.useFakeTimers() and vi.advanceTimersByTime() for timer-dependent code
  • Call vi.useRealTimers() in afterEach when using fake timers
  • Use vi.waitFor() for testing async state changes

Coverage

  • Configure coverage in vitest.config.ts: coverage: { provider: 'v8', reporter: ['text', 'html'] }
  • Set coverage thresholds: statements: 80, branches: 80, functions: 80
  • Exclude test files, configs, and type files from coverage
  • Focus coverage on business logic, not framework glue code

Snapshot Testing

  • Use toMatchInlineSnapshot() over toMatchSnapshot() — review changes in-place
  • Snapshot simple serializable output only — not DOM trees or large objects
  • Update snapshots intentionally with --update, never blindly
  • Use expect().toMatchFileSnapshot() for large expected outputs

Read the full file on GitHub · 57 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 57 lines · 647 tokens per session scan A 8fb1b16596a3

Subscribe to this mod's changes

vitest is a cursor rule published in the GitHub repository nedcodes-ok/cursor-doctor (9 stars, last pushed 5mo ago), licensed MIT. It adds 647 tokens to every session, about $0.0032 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.