name: jest description: 提供Jest测试框架的全面指南,包括测试编写、匹配器、异步测试、模拟、快照、配置和CLI。当用户询问Jest、需要编写JavaScript/TypeScript测试、模拟依赖项或为项目配置Jest时使用。 license: 完整条款在LICENSE.txt中
何时使用此技能
使用此技能当用户想要:
- 使用Jest编写单元测试和集成测试
- 在项目中设置Jest
- 使用Jest匹配器和断言
- 测试异步代码
- 模拟函数、模块和依赖项
- 创建快照测试
- 为不同环境配置Jest
- 将Jest与TypeScript、React、Vue或其他框架一起使用
- 运行测试并生成覆盖率报告
- 使用Jest CLI命令
- 设置测试环境(jsdom、node)
- 将Jest与Vite一起使用(通过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 - 命令行 →
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- 模拟函数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