跳到正文
Skillpedia
测试75k · #2941

mock-best-practices

它能做什么

这项技能检查测试文件如何使用模拟对象,即代码外部部分的替身。适用于希望测试能发现真实问题、而不只是通过的开发者。

适用场景

  • ✓当你请智能体检查测试文件如何使用模拟对象的时候。
  • ✓当测试通过但真实应用出错,而你怀疑模拟的范围太大的时候。
  • ✓当你在编写新测试,并希望了解哪些部分该模拟、哪些应保持真实的时候。

技能链接

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/mock-best-practices/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/mock-best-practices/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#2941
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能