跳到正文
Skillpedia
测试40k · #7262

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

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

可以这样问 AI

“如何测试一个会调用外部 API 的函数?”

每周 5 个新技能,1 分钟读完。

工作原理

工具会把测试组织到特定的文件中,并使用 `describe` 和 `it` 等命令来组织测试结构。为了模拟依赖,它会使用 `jest.mock` 和 `jest.spyOn` 等函数创建这些依赖的假版本,让你可以孤立地测试自己的代码。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Test Structure

  • Name test files with .test.ts or .test.js suffix
  • 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() or mockReturnValue() to define mock behavior
  • Reset mocks between tests with jest.resetAllMocks() in afterEach

Testing Async Code

  • Always return promises or use async/await syntax in tests
  • Use resolves/rejects matchers 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 userEvent over fireEvent for 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能