关于 jest
面向使用 Jest 的开发者的文档型技能,将常见测试任务对应到示例、API 参考和模板。
主要功能
- 01单元与集成测试指南
- 02匹配器与断言
- 03异步测试与 Mock
- 04快照测试与覆盖率
- 05配置和 CLI 参考
- 06TypeScript 与框架示例
适用场景
- 01在项目中配置 Jest
- 02测试异步行为
- 03模拟模块和依赖
- 04配置测试环境
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ full-stack-skills/ testing-skills --skill jest安装后在 Claude Code 中输入/jest确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSE.txtagents/openai.yamlapi/cli.mdexpect.mdjest-object.mdmock-functions.mdtest.mdexamples/configuration/jest-config.mdexamples/getting-started/installation.mdusing-matchers.mdexamples/testing/asynchronous.mdmanual-mocks.mdmock-functions.mdsetup-teardown.mdsnapshot-testing.mdtimer-mocks.mdtemplates/jest-config.mdtest-template.md详细介绍
适用场景
在用户希望执行以下操作时使用此技能:
- 使用 Jest 编写单元测试和集成测试
- 在项目中设置 Jest
- 使用 Jest 匹配器和断言
- 测试异步代码
- 模拟函数、模块和依赖项
- 创建快照测试
- 为不同环境配置 Jest
- 将 Jest 与 TypeScript、React、Vue 或其他框架配合使用
- 运行测试并生成覆盖率报告
- 使用 Jest CLI 命令
- 设置测试环境(jsdom、node)
- 在 Vite 项目中使用 Jest(通过 vite-jest,但对于新的 Vite 项目,建议使用 Vitest)
如何使用此技能
此技能按照 Jest 官方文档结构组织(https://jestjs.io/docs/getting-started、https://jestjs.io/docs/api)。使用 Jest 时:
-
根据用户请求确定主题:
- 入门/快速开始 →
examples/getting-started/installation.md或examples/getting-started/using-matchers.md - 测试/测试 →
examples/testing/asynchronous.md或examples/testing/setup-teardown.md - 模拟函数/Mock 函数 →
examples/testing/mock-functions.md - 模拟模块/Mock 模块 →
examples/testing/manual-mocks.md - 快照/快照 →
examples/testing/snapshot-testing.md - 配置/配置 →
examples/configuration/jest-config.md - CLI/命令行 →
examples/configuration/cli-options.md
- 入门/快速开始 →
-
从
examples/目录加载合适的示例文件:入门(快速开始)-
examples/getting-started/:examples/getting-started/installation.md- 安装 Jest 并进行基本设置examples/getting-started/using-matchers.md- 使用 Jest 匹配器和断言
测试(测试)-
examples/testing/:examples/testing/asynchronous.md- 测试异步代码examples/testing/setup-teardown.md- 设置和清理函数examples/testing/mock-functions.md- 模拟函数examples/testing/manual-mocks.md- 手动模拟examples/testing/snapshot-testing.md- 快照测试examples/testing/timer-mocks.md- 模拟计时器examples/testing/es6-class-mocks.md- 模拟 ES6 类
配置(配置)-
examples/configuration/:examples/configuration/jest-config.md- Jest 配置选项examples/configuration/cli-options.md- CLI 选项和标志examples/configuration/babel.md- 将 Jest 与 Babel 配合使用examples/configuration/typescript.md- 将 Jest 与 TypeScript 配合使用
指南(指南)-
examples/guides/:examples/guides/migrating-to-jest.md- 迁移到 Jestexamples/guides/testing-react-apps.md- 测试 React 应用examples/guides/testing-vue-apps.md- 测试 Vue 应用
-
遵循相应示例文件中的具体说明,了解语法、结构和最佳实践
重要提示:
- 所有示例均遵循 Jest 最佳实践
- 在适用的情况下,示例同时提供 JavaScript 和 TypeScript 版本
- 每个示例文件都包含核心概念、代码示例和要点
- 务必查看示例文件,了解最佳实践和常见模式
-
必要时参考
api/目录中的 API 文档:api/expect.md- expect() API 和匹配器api/mock-functions.md- Mock 函数 APIapi/jest-object.md- Jest 对象 APIapi/test.md- test() 和 describe() APIapi/cli.md- CLI 命令和选项
-
使用
templates/目录中的模板:templates/jest-config.md- Jest 配置模板templates/test-template.md- 测试文件模板templates/setup-file.md- 设置文件模板
文档映射(与 https://jestjs.io/docs/getting-started、https://jestjs.io/docs/api 一一对应)
快速开始(快速开始):
examples/getting-started/installation.md→ https://jestjs.io/docs/getting-startedexamples/getting-started/using-matchers.md→ https://jestjs.io/docs/using-matchers
测试(测试):
examples/testing/asynchronous.md→ https://jestjs.io/docs/asynchronousexamples/testing/setup-teardown.md→ https://jestjs.io/docs/setup-teardownexamples/testing/mock-functions.md→ https://jestjs.io/docs/mock-functionsexamples/testing/manual-mocks.md→ https://jestjs.io/docs/manual-mocksexamples/testing/snapshot-testing.md→ https://jestjs.io/docs/snapshot-testingexamples/testing/timer-mocks.md→ https://jestjs.io/docs/timer-mocksexamples/testing/es6-class-mocks.md→ https://jestjs.io/docs/es6-class-mocks
配置(配置):
examples/configuration/jest-config.md→ https://jestjs.io/docs/configurationexamples/configuration/cli-options.md→ https://jestjs.io/docs/cli
API 参考:
api/expect.md→ https://jestjs.io/docs/expectapi/mock-functions.md→ https://jestjs.io/docs/mock-functionsapi/jest-object.md→ https://jestjs.io/docs/jest-objectapi/test.md→ https://jestjs.io/docs/api#testname-fn-timeoutapi/cli.md→ https://jestjs.io/docs/cli
示例和模板
此技能包含详细示例,按官方文档结构组织。所有示例都位于 examples/ 目录中(参见上方映射)。
使用示例:
- 根据用户请求确定主题
- 从上方映射中加载相应的示例文件
- 遵循该文件中的说明、语法和最佳实践
- 根据具体用例调整代码示例
使用模板:
- 参考
templates/目录中的常用脚手架模板 - 根据具体需求和编码风格调整模板
最佳实践
- 编写描述性测试名称:使用清晰且具有描述性的测试名称
- 使用合适的匹配器:为每个断言选择正确的匹配器
- 模拟外部依赖:模拟外部 API、数据库和服务
- 测试边界情况:测试正常路径和错误情况
- 保持测试隔离:每个测试都应相互独立
- 使用设置和清理:正确清理资源
- 快照测试:对 UI 组件使用快照,但要检查变更
- 覆盖率目标:追求有意义的覆盖率,而不只是较高的百分比
- 组织测试:使用 describe 块对相关测试进行分组
- 异步测试:使用 async/await 或 promises 正确处理异步代码
资源
- 官方文档:https://jestjs.io/
- 快速开始:https://jestjs.io/docs/getting-started
- API 参考:https://jestjs.io/docs/api
- GitHub 仓库:https://github.com/jestjs/jest
关键词
Jest、测试、单元测试、集成测试、Mock、spy、快照、匹配器、断言、异步测试、设置、清理、覆盖率、CLI、配置、TypeScript、React、Vue、Babel、测试、单元测试、集成测试、Mock、快照、匹配器、断言、异步测试、覆盖率、配置
评论
支持 Markdown