testing-patterns
使用 Jest 模式编写简洁测试
也可从以下获取: 0xDarkMatter,0xDarkMatter,sickn33,CleanExpo
当测试不一致、过于复杂或测试实现细节时,会变得难以维护。此技能提供经过验证的 Jest 模式,包括工厂函数、模拟策略和 TDD 工作流。
下载技能 ZIP
在 Claude 中上传
前往 设置 → 功能 → 技能 → 上传技能
开启并开始使用
测试它
正在使用“testing-patterns”。 为 Jest 测试中的用户数据创建工厂函数
预期结果:
- 定义一个工厂函数,创建具有合理默认值的模拟用户对象
- 允许部分覆盖,以便每个测试可以自定义特定属性
- 使用 TypeScript 接口确保测试套件中的类型安全
- 示例: const getMockUser = (overrides) => ({ id: '123', name: 'John Doe', ...overrides })
正在使用“testing-patterns”。 如何在 Jest 中模拟 GraphQL hook
预期结果:
- 使用 jest.mock() 拦截 GraphQL hook 的导入
- 返回一个工厂函数,生成可配置的模拟实现
- 使用 mockReturnValue 或 mockResolvedValue 在每个测试中控制返回值
- 在测试之间使用 jest.clearAllMocks() 清除模拟
正在使用“testing-patterns”。 指导我为登录组件进行 TDD
预期结果:
- 步骤 1: 编写一个描述预期登录行为的失败测试
- 步骤 2: 实现最少的代码使测试通过
- 步骤 3: 在保持测试通过的同时重构代码
- 步骤 4: 为每个新的登录功能或边缘情况重复此过程
安全审计
安全Pure documentation skill containing Jest testing patterns. Contains only markdown documentation with TypeScript code examples. No executable code, scripts, network calls, file system access, or external commands. Static findings are false positives triggered by pattern matching on 'mock', backticks in markdown, and testing-related terminology.
风险因素
🌐 网络访问 (1)
📁 文件系统访问 (1)
⚙️ 外部命令 (24)
质量评分
你能构建什么
编写可维护的测试
使用工厂函数和适当的模拟策略创建一致、可读的测试。
遵循 TDD 工作流
应用红-绿-重构循环,并指导如何先编写失败测试。
组织测试套件结构
使用 describe 块组织测试并有效测试用户交互。
试试这些提示
Create a factory function for mocking User in Jest tests with sensible defaults and override capability.
Show how to mock a module in Jest with a factory function that returns configurable mock functions.
Guide me through writing a failing test first for a component that meets these requirements. Then implement minimal code to pass.
Write a complete Jest test suite for a component with proper describe block organization covering rendering, user interactions, and edge cases.
最佳实践
- 在实现生产代码之前先编写失败测试
- 使用描述预期行为的测试名称
- 测试公共 API 和业务需求,而非实现细节
- 在测试之间使用 jest.clearAllMocks() 清除模拟
避免
- 测试模拟行为而非实际组件行为
- 重复测试数据而非使用工厂函数
- 测试可能在不影响业务的情况下改变的实现细节
- 跳过边缘情况或错误状态的测试