javascript-typescript-jest
它能做什么
这个工具可以帮你使用 Jest 库为 JavaScript 和 TypeScript 代码编写测试。它会讲解如何组织测试、如何模拟依赖其他部分的代码(比如外部 API),以及如何验证一切是否按预期工作。
适用场景
- ✓当你编写 JavaScript 或 TypeScript 代码时。
- ✓当你修改代码后,想确认它仍然正常工作时。
- ✓当你想模拟外部依赖、只测试自己的逻辑时。
技能链接
https://github.com/github/awesome-copilot/blob/main/skills/javascript-typescript-jest/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“如何测试一个会调用外部 API 的函数?”
每周 5 个新技能,1 分钟读完。
工作原理
工具会把测试组织到特定的文件中,并使用 `describe` 和 `it` 等命令来组织测试结构。为了模拟依赖,它会使用 `jest.mock` 和 `jest.spyOn` 等函数创建这些依赖的假版本,让你可以孤立地测试自己的代码。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Test Structure
- Name test files with
.test.tsor.test.jssuffix - Place test files next to the code they test or in a dedicated
__tests__directory - Use descriptive test names that explain the expected behavior
- Use nested describe blocks to organize related tests
- Follow the pattern:
describe('Component/Function/Class', () => { it('should do something', () => {}) })
Effective Mocking
- Mock external dependencies (APIs, databases, etc.) to isolate your tests
- Use
jest.mock()for module-level mocks - Use
jest.spyOn()for specific function mocks - Use
mockImplementation()ormockReturnValue()to define mock behavior - Reset mocks between tests with
jest.resetAllMocks()inafterEach
Testing Async Code
- Always return promises or use async/await syntax in tests
- Use
resolves/rejectsmatchers for promises - Set appropriate timeouts for slow tests with
jest.setTimeout()
Snapshot Testing
- Use snapshot tests for UI components or complex objects that change infrequently
- Keep snapshots small and focused
- Review snapshot changes carefully before committing
Testing React Components
- Use React Testing Library over Enzyme for testing components
- Test user behavior and component accessibility
- Query elements by accessibility roles, labels, or text content
- Use
userEventoverfireEventfor more realistic user interactions
Common Jest Matchers
- Basic:
expect(value).toBe(expected),expect(value).toEqual(expected) - Truthiness:
expect(value).toBeTruthy(),expect(value).toBeFalsy() - Numbers:
expect(value).toBeGreaterThan(3),expect(value).toBeLessThanOrEqual(3) - Strings:
expect(value).toMatch(/pattern/),expect(value).toContain('substring') - Arrays:
expect(array).toContain(item),expect(array).toHaveLength(3) - Objects:
expect(object).toHaveProperty('key', value) - Exceptions:
expect(fn).toThrow(),expect(fn).toThrow(Error) - Mock functions:
expect(mockFn).toHaveBeenCalled(),expect(mockFn).toHaveBeenCalledWith(arg1, arg2)
常见问题
什么是“mock”(模拟对象)?
Mock 是代码中某些部分(比如函数或模块)的假版本,用于测试。你可以用它控制这些部分的行为,从而在不依赖外部或复杂系统的情况下测试代码。
为什么要写测试?
测试能帮你确认代码按预期工作,并在修改代码时快速发现错误,让你更放心地确信代码不会出问题。
什么是“快照测试”(snapshot testing)?
快照测试是一种用于测试用户界面或复杂数据结构的技术。它会先保存一份预期输出的“快照”,之后的测试会把当前输出和快照进行比较,看看是否出现了意外的改动。
详情
作者的原始描述
Best practices for writing JavaScript/TypeScript tests using Jest, including mocking strategies, test structure, and common patterns.
repo: github/awesome-copilot
热度
- GitHub 星标
- 40k
- 排名
- #7262
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
acquire-codebase-knowledge
40k帮你的智能体理解并编写现有代码项目的文档。它有助于新成员快速上手,或者快速了解项目架构。
acreadiness-assess
40k让你的智能体评估一个项目对 AI 的准备程度,并生成报告。它有助于了解项目在使用 AI 工具方面的准备情况。
acreadiness-generate-instructions
40k帮你的智能体根据评估结果编写 AI 工具的指令文件。它有助于改善 AI 与代码的交互方式,并弥补已发现的不足。
acreadiness-policy
40k让你的智能体协助制定或应用 AI 项目评估的政策。它有助于规范项目的评估方式,并确保合规。