vitest-best-practices-auto

A set of rules for writing unit tests with Vitest, a JavaScript and TypeScript testing tool. It covers test structure, naming, mocks, asynchronous code, cleanup, and test organization.

In plain words
What is it for?
Use it when creating or reviewing Vitest unit tests, especially tests involving mocks, timers, shared setup, or asynchronous code.
Why use it?
It helps prevent unclear, unreliable, or order-dependent tests by applying consistent practices while tests are written.

Cursor rule for Cursor

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/usrrname/cursorrules/vitest-best-practices-auto
Clone the repo
git clone --depth 1 https://github.com/usrrname/cursorrules

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 905 The whole file, excluding the scripts and references it only reads on demand.
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.00000 $0.00905
Opus 5 $0.00000 $0.00452
Sonnet 5 $0.00000 $0.00181
Haiku 4.5 $0.00000 $0.00090

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

Security

Grade A, and why

vitest-best-practices-auto 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.

.cursor/rules/test/vitest-best-practices-auto.mdc · 101 lines

How it starts

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

Vitest Unit Testing Best Practices

This rule enforces and suggests best practices when writing Vitest unit tests to ensure reliable, maintainable, and expressive test suites.

Critical Rules

  • Tests must use clear and descriptive names (it/test strings).
  • Use "Arrange, Act, Assert" pattern for writing unit tests.
  • Use describe blocks to group related tests logically.
  • Use beforeEach/afterEach for shared setup/teardown instead of duplicating code.
  • Cleanup side-effects using vi.resetAllMocks() or vi.restoreAllMocks().
  • Prefer vi.fn() or vi.spyOn() over manually created stubs.
  • Avoid global state – never rely on test execution order.
  • For asynchronous code, always await promises and/or use expect.assertions(n).
  • Do not snapshot complex objects blindly; snapshot only stable, meaningful output.
  • Use vi.useFakeTimers() cautiously and always restore real timers with vi.useRealTimers() afterwards.
  • Do not disable tests with test.skip/it.skip; instead, fix or remove them.
  • Keep test files co-located with source or in a dedicated __tests__ folder.
  • Ensure each test has at least one expect call (or uses .resolves/.rejects).
  • Prefer explicit assertions over implicit truthiness checks.

filters:

Match Vitest test files

  • type: file_extension pattern: "\.(test|spec)\.(js|ts|jsx|tsx)$"

Warn if test title is non-descriptive (too short)

  • type: content pattern: "\b(?:it|test)\((?:'|")\s*[A-Za-z0-9]{1,10}\s*(?:'|")"

Prevent skipped tests

  • type: content pattern: "\b(?:it|test|describe)\.skip\("

Ensure at least one expect‡ per test block (heuristic)

  • type: content pattern: "\b(?:it|test)\([^)]{(?:(?!expect).)}$"

actions:

Suggest more descriptive titles

  • type: suggest message: | Test titles should be descriptive. Use behaviour-driven phrases, e.g. it('renders submit button when form is valid', ...).

Read the full file on GitHub · 101 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 · 101 lines · 905 tokens per session scan A 9a210c8318b2

Subscribe to this mod's changes

vitest-best-practices-auto is a cursor rule published in the GitHub repository usrrname/cursorrules (10 stars, last pushed 4mo ago), licensed ISC. It costs nothing until one of its globs matches a file; then it loads 905 tokens. 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.