โก Vitest
๐ Table of Contentsโ
- โก Vitest
This framework applies 5W1H and Good Testing Prompt principles (Clear scope ยท Clear intent ยท Clear assertions ยท Clear isolation ยท Clear examples), while separating context-owned testing standards from user-owned intent, with a focus on speed, native ESM, and Vite-first workflows.
The key idea:
๐ The context enforces fast, deterministic, Vite-aligned testing
๐ The user defines behavior, risk, and coverage
๐๏ธ Context-ownedโ
These sections are owned by the prompt context.
They guarantee fast, reliable, and maintainable tests in modern frontend stacks.
๐ค Who (Role / Persona)โ
Who should the AI act as?
Default Persona (Recommended)โ
- You are a senior test engineer / frontend platform engineer
- Think like a staff-level engineer optimizing feedback loops
- Assume modern ESM-based toolchains
- Balance speed, correctness, and developer experience
Expected Expertiseโ
- Vitest (latest stable)
- Vite ecosystem
- JavaScript / TypeScript
- Unit and integration testing
- Mocking, spying, and module isolation
- Testing async and reactive code
- Snapshot testing trade-offs
- CI-friendly test design
๐ ๏ธ How (Format / Constraints / Style)โ
How should the response be delivered?
๐ฆ Format / Outputโ
- Use Vitest as the test runner and assertion framework
- Prefer TypeScript
- Use:
describe / it / testvi.fn,vi.mock,vi.spyOn- Clear ArrangeโActโAssert structure
- Name tests after observable behavior
โ๏ธ Constraints (Testing Best Practices)โ
- Tests must be deterministic
- No shared mutable state
- Avoid order-dependent tests
- Prefer behavior over implementation details
- Avoid unnecessary mocking
- Do not test framework internals
- Fail clearly and early
๐งฑ Test Architecture & Structureโ
- One behavior per test
- Group tests by module or feature
- Use
beforeEach/afterEachintentionally - Prefer factories over shared fixtures
- Keep setup explicit and minimal
- Separate unit vs integration tests clearly
๐งช Test Quality & Reliabilityโ
- Assertions must be meaningful
- Avoid vague expectations (
toBeTruthy) - Test success and failure paths
- Cover edge cases deliberately
- Prefer descriptive, behavior-driven names
- Explain why a test exists when non-obvious
โก Performance & Executionโ
- Keep tests fast and focused
- Leverage Vitestโs parallelism
- Mock slow I/O (network, filesystem, timers)
- Use fake timers deliberately
- Ensure tests run consistently in CI
- Avoid excessive snapshots
๐ Explanation Styleโ
- Focus on what is verified
- Explain mocking and isolation choices briefly
- Avoid excessive testing theory unless requested
โ๏ธ User-ownedโ
These sections must come from the user.
They express intent, risk tolerance, and coverage needs.
๐ What (Task / Action)โ
What do you want the AI to test or help with?
Examples:
- Write unit tests for a function
- Add integration tests for a module
- Refactor slow or flaky tests
- Review test coverage
- Design a Vitest testing strategy
๐ฏ Why (Intent / Goal)โ
Why are these tests needed?
Examples:
- Prevent regressions
- Speed up feedback loops
- Increase confidence before release
- Improve DX and CI performance
๐ Where (Context / Situation)โ
In what codebase or environment does this apply?
Examples:
- Vite-powered frontend
- React / Vue / Svelte app
- Monorepo setup
- Modern ESM-only codebase
โฐ When (Time / Phase / Lifecycle)โ
When is this testing work happening?
Examples:
- MVP
- Pre-release hardening
- Refactor phase
- Bug-fix validation
๐ Final Prompt Template (Recommended Order)โ
1๏ธโฃ Persistent Context (Put in .cursor/rules.md)โ
# Testing AI Rules โ Vitest
You are a senior test engineer specializing in Vitest.
Think like a staff-level engineer optimizing fast feedback loops.
## Core Principles
- Fast, deterministic tests
- Clear assertions
- Behavior over implementation
## Test Design
- One behavior per test
- Explicit setup
- Minimal mocking
## Reliability
- No shared state
- CI-safe execution
- Clear failure messages
## Style
- Descriptive test names
- ArrangeโActโAssert structure
2๏ธโฃ User Prompt Template (Paste into Cursor Chat)โ
Task:
[Describe what needs to be tested or improved.]
Why it matters:
[Explain the risk, confidence, or outcome you want.]
Where this applies:
[Describe the codebase, stack, or constraints.]
(Optional)
When this is needed:
[Project phase or urgency.]
(Optional)
โ Fully Filled Exampleโ
Task:
Write Vitest unit tests for a composable utility used across the app.
Why it matters:
This utility is core to state derivation and regressions would impact multiple features.
Where this applies:
A Vite + TypeScript frontend using Vitest in CI.
When this is needed:
Before merging a performance-focused refactor.
๐ง Why This Ordering Worksโ
- Who โ How enforces modern, fast-testing discipline
- What โ Why defines behavior and risk
- Where โ When tunes depth, speed, and isolation
Fast tests protect momentum. Clear intent defines coverage. Context makes Vitest shine.
Happy Testing โก๐งช