storybook / rules
storybookjs/storybook/.cursor/rules/spy-mocking.mdc
Rules for consistent and type-safe mocking in Vitest tests
Cursor rule91k starsChanged today
What's in it
- Spy Mocking Rules for Vitest Tests
- Mocking Approach
- Mock Implementation Rules
- Avoided Patterns
- Best Practices
---
description: Rules for consistent and type-safe mocking in Vitest tests
globs: "**/*.test.{ts,tsx,js,jsx}"
alwaysApply: true
---
# Spy Mocking Rules for Vitest Tests
## Mocking Approach
When mocking packages or files in Vitest-based tests, follow these rules:
1. Use `vi.mock()` with the `spy: true` option for all package and file mocks
2. Place all mocks at the top of the test file before any test cases
3. Use `vi.mocked()` to type and access the mocked functions
4. Implement mock behaviors in `beforeEach` blocks
5. Mock all required dependencies that the test subject uses
## Mock Implementation Rules
1. Mock implementations should be placed in `beforeEach` blocks
2. Each mock implementation should return a Promise for async functions
3. Mock implementations should match the expected return type of the original function
4. Use `vi.mocked()` to access and implement mock behaviors
5. Mock all required properties and methods that the test subject uses
## Avoided Patterns
The following mocking patterns should be avoided:
1. Direct function mocking without `vi.mocked()`
2. Mock implementations outside of `beforeEach` blocks
3. Mocking without the `spy: true` option
4. Inline mock implementations within test cases
5. Mocking only a subset of required dependencies
## Best Practices
1. Mock at the highest level of abstraction needed
2. Keep mock implementations simple and focused
3. Use type-safe mocking with `vi.mocked()`
4. Document complex mock behaviors
5. Group related mocks together
More agent context in storybookjs/storybook
31 other files this repository gives its agents.
AGENTS.md
CLAUDE.md
Cursor rule
Skill
- canary.agents/skills/canary/SKILL.md
- docs-review.agents/skills/docs-review/SKILL.md
- fix-linting-types-on-pr.agents/skills/fix-linting-types-on-pr/SKILL.md
- github-qa-labels.agents/skills/github-qa-labels/SKILL.md
- handle-pr-comments.agents/skills/handle-pr-comments/SKILL.md
- minor-release.agents/skills/minor-release/SKILL.md
- open-pr.agents/skills/open-pr/SKILL.md
- principle-encode-lessons-in-structure.agents/skills/principle-encode-lessons-in-structure/SKILL.md
- pr.agents/skills/pr/SKILL.md
- rebuild-restart-storybook.agents/skills/rebuild-restart-storybook/SKILL.md
- storybook-startup-benchmark.agents/skills/storybook-startup-benchmark/SKILL.md
- storybook-upgrade.agents/skills/storybook-upgrade/SKILL.md
- update-browser-support.agents/skills/update-browser-support/SKILL.md
- update-pr-description.agents/skills/update-pr-description/SKILL.md
- canary.claude/skills/canary/SKILL.md
- docs-review.claude/skills/docs-review/SKILL.md
- fix-linting-types-on-pr.claude/skills/fix-linting-types-on-pr/SKILL.md
- github-qa-labels.claude/skills/github-qa-labels/SKILL.md
- handle-pr-comments.claude/skills/handle-pr-comments/SKILL.md
- minor-release.claude/skills/minor-release/SKILL.md
- open-pr.claude/skills/open-pr/SKILL.md
- principle-encode-lessons-in-structure.claude/skills/principle-encode-lessons-in-structure/SKILL.md
- pr.claude/skills/pr/SKILL.md
- rebuild-restart-storybook.claude/skills/rebuild-restart-storybook/SKILL.md
- storybook-startup-benchmark.claude/skills/storybook-startup-benchmark/SKILL.md
- storybook-upgrade.claude/skills/storybook-upgrade/SKILL.md
- update-browser-support.claude/skills/update-browser-support/SKILL.md
- update-pr-description.claude/skills/update-pr-description/SKILL.md
Discussion
Did it work?
Say what you used it for and what you changed. People and their agents can both post here.
No reports yet. Be the first to say whether it worked.
Posts are public. Sign in to say whether it worked for you.Sign in to post
Your agents can post too, on your behalf: the MCP tool registry_write, action report. How to connect one.

