mock-best-practices
它能做什么
这项技能检查测试文件如何使用模拟对象,即代码外部部分的替身。适用于希望测试能发现真实问题、而不只是通过的开发者。
适用场景
- ✓当你请智能体检查测试文件如何使用模拟对象的时候。
- ✓当测试通过但真实应用出错,而你怀疑模拟的范围太大的时候。
- ✓当你在编写新测试,并希望了解哪些部分该模拟、哪些应保持真实的时候。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/mock-best-practices/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的测试都通过了,但应用仍然出错。请帮我检查是不是模拟得太多了。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会阅读测试文件,查找会隐藏业务逻辑的模拟、缺失的清理,以及检查内部细节的测试。然后它会建议删除不必要的模拟,并在合适的层级进行模拟。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Follow mocking best practices
Proper mocking isolates units under test while keeping tests realistic—over-mocking creates false confidence when tests pass but production breaks.
Quick Reference
- Mock external dependencies (APIs, databases, file system), not business logic
- Always clean up mocks with jest.restoreAllMocks() in afterEach
- Use MSW for realistic API mocking without coupling to implementation
- Test behavior and outputs, not internal implementation details
Check
Review this test file for mocking patterns, checking for over-mocking, missing mock cleanup, and proper mock implementation.
Fix
Improve mocking strategy by removing unnecessary mocks, adding proper cleanup, and mocking at the right level of abstraction.
Explain
Explain mocking best practices including when to mock, what to mock, and common mocking pitfalls.
Code Review
Review tests, CI workflows, and enforcement points related to Follow mocking best practices. Flag exact gaps where the rule is not automatically verified or where failures do not block regressions.
For full implementation details, code examples, and framework-specific guidance,
see references/rule.md.
Rule page: https://frontendchecklist.io/rules/testing/mock-best-practices
常见问题
应该模拟什么?
这项技能建议模拟外部依赖,例如 API、数据库和文件系统,而不是业务逻辑。
什么是 MSW?
它是一种用于逼真 API 模拟的工具,不依赖于代码调用 API 的方式。
为什么要清理模拟?
在测试之间保留的模拟可能会改变结果,使失败难以理解。
详情
作者的原始描述
Use when reviewing CI coverage, automated checks, or test strategy related to Follow mocking best practices. Focus on whether the rule is continuously verified, not just documented.
热度
- GitHub 星标
- 75k
- 排名
- #2941
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。