jest
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWhen to use this skill
何时使用本技能
Use this skill whenever the user wants to:
- Write unit tests and integration tests with Jest
- Set up Jest in a project
- Use Jest matchers and assertions
- Test asynchronous code
- Mock functions, modules, and dependencies
- Create snapshot tests
- Configure Jest for different environments
- Use Jest with TypeScript, React, Vue, or other frameworks
- Run tests and generate coverage reports
- Use Jest CLI commands
- Set up test environments (jsdom, node)
- Use Jest with Vite (via vite-jest, though Vitest is recommended for new Vite projects)
当用户有以下需求时,可使用本技能:
- 使用Jest编写单元测试和集成测试
- 在项目中搭建Jest环境
- 使用Jest匹配器和断言
- 测试异步代码
- Mock函数、模块和依赖项
- 创建快照测试
- 为不同环境配置Jest
- 在TypeScript、React、Vue或其他框架中使用Jest
- 运行测试并生成覆盖率报告
- 使用Jest CLI命令
- 搭建测试环境(jsdom、node)
- 在Vite项目中使用Jest(通过vite-jest,不过新Vite项目推荐使用Vitest)
How to use this skill
如何使用本技能
This skill is organized to match the Jest official documentation structure (https://jestjs.io/docs/getting-started, https://jestjs.io/docs/api). When working with Jest:
-
Identify the topic from the user's request:
- Getting started/快速开始 → or
examples/getting-started/installation.mdexamples/getting-started/using-matchers.md - Testing/测试 → or
examples/testing/asynchronous.mdexamples/testing/setup-teardown.md - Mock functions/Mock 函数 →
examples/testing/mock-functions.md - Mock modules/Mock 模块 →
examples/testing/manual-mocks.md - Snapshots/快照 →
examples/testing/snapshot-testing.md - Configuration/配置 →
examples/configuration/jest-config.md - CLI/命令行 →
examples/configuration/cli-options.md
- Getting started/快速开始 →
-
Load the appropriate example file from thedirectory:
examples/Getting Started (快速开始) -:examples/getting-started/- - Installing Jest and basic setup
examples/getting-started/installation.md - - Using Jest matchers and assertions
examples/getting-started/using-matchers.md
Testing (测试) -:examples/testing/- - Testing asynchronous code
examples/testing/asynchronous.md - - Setup and teardown functions
examples/testing/setup-teardown.md - - Mocking functions
examples/testing/mock-functions.md - - Manual mocks
examples/testing/manual-mocks.md - - Snapshot testing
examples/testing/snapshot-testing.md - - Mocking timers
examples/testing/timer-mocks.md - - Mocking ES6 classes
examples/testing/es6-class-mocks.md
Configuration (配置) -:examples/configuration/- - Jest configuration options
examples/configuration/jest-config.md - - CLI options and flags
examples/configuration/cli-options.md - - Using Jest with Babel
examples/configuration/babel.md - - Using Jest with TypeScript
examples/configuration/typescript.md
Guides (指南) -:examples/guides/- - Migrating to Jest
examples/guides/migrating-to-jest.md - - Testing React applications
examples/guides/testing-react-apps.md - - Testing Vue applications
examples/guides/testing-vue-apps.md
-
Follow the specific instructions in that example file for syntax, structure, and best practicesImportant Notes:
- All examples follow Jest best practices
- Examples include both JavaScript and TypeScript versions where applicable
- Each example file includes key concepts, code examples, and key points
- Always check the example file for best practices and common patterns
-
Reference API documentation in thedirectory when needed:
api/- - expect() API and matchers
api/expect.md - - Mock functions API
api/mock-functions.md - - Jest object API
api/jest-object.md - - test() and describe() API
api/test.md - - CLI commands and options
api/cli.md
-
Use templates from thedirectory:
templates/- - Jest configuration templates
templates/jest-config.md - - Test file templates
templates/test-template.md - - Setup file templates
templates/setup-file.md
本技能的组织结构与Jest官方文档结构一致(https://jestjs.io/docs/getting-started,https://jestjs.io/docs/api)。使用Jest时,请遵循以下步骤:
-
从用户的请求中确定主题:
- 快速开始 → 或
examples/getting-started/installation.mdexamples/getting-started/using-matchers.md - 测试 → 或
examples/testing/asynchronous.mdexamples/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/- - Jest安装与基础设置
examples/getting-started/installation.md - - Jest匹配器与断言的使用
examples/getting-started/using-matchers.md
测试 -:examples/testing/- - 异步代码测试
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 - - Mock计时器
examples/testing/timer-mocks.md - - Mock ES6类
examples/testing/es6-class-mocks.md
配置 -:examples/configuration/- - Jest配置选项
examples/configuration/jest-config.md - - CLI选项与参数
examples/configuration/cli-options.md - - 在Babel中使用Jest
examples/configuration/babel.md - - 在TypeScript中使用Jest
examples/configuration/typescript.md
指南 -:examples/guides/- - 迁移至Jest
examples/guides/migrating-to-jest.md - - React应用测试
examples/guides/testing-react-apps.md - - Vue应用测试
examples/guides/testing-vue-apps.md
-
遵循示例文件中的具体说明,包括语法、结构和最佳实践重要说明:
- 所有示例均遵循Jest最佳实践
- 适用的示例同时包含JavaScript和TypeScript版本
- 每个示例文件都包含核心概念、代码示例和关键点
- 请务必查看示例文件以了解最佳实践和常见模式
-
必要时参考目录中的API文档:
api/- - expect() API与匹配器
api/expect.md - - Mock函数API
api/mock-functions.md - - Jest对象API
api/jest-object.md - - test()与describe() API
api/test.md - - CLI命令与选项
api/cli.md
-
使用目录中的模板:
templates/- - Jest配置模板
templates/jest-config.md - - 测试文件模板
templates/test-template.md - - 初始化文件模板
templates/setup-file.md
Doc mapping (one-to-one with https://jestjs.io/docs/getting-started, https://jestjs.io/docs/api)
Getting Started (快速开始):
- → https://jestjs.io/docs/getting-started
examples/getting-started/installation.md - → https://jestjs.io/docs/using-matchers
examples/getting-started/using-matchers.md
Testing (测试):
- → https://jestjs.io/docs/asynchronous
examples/testing/asynchronous.md - → https://jestjs.io/docs/setup-teardown
examples/testing/setup-teardown.md - → https://jestjs.io/docs/mock-functions
examples/testing/mock-functions.md - → https://jestjs.io/docs/manual-mocks
examples/testing/manual-mocks.md - → https://jestjs.io/docs/snapshot-testing
examples/testing/snapshot-testing.md - → https://jestjs.io/docs/timer-mocks
examples/testing/timer-mocks.md - → https://jestjs.io/docs/es6-class-mocks
examples/testing/es6-class-mocks.md
Configuration (配置):
- → https://jestjs.io/docs/configuration
examples/configuration/jest-config.md - → https://jestjs.io/docs/cli
examples/configuration/cli-options.md
API Reference:
- → https://jestjs.io/docs/expect
api/expect.md - → https://jestjs.io/docs/mock-functions
api/mock-functions.md - → https://jestjs.io/docs/jest-object
api/jest-object.md - → https://jestjs.io/docs/api#testname-fn-timeout
api/test.md - → https://jestjs.io/docs/cli
api/cli.md
快速开始:
- → https://jestjs.io/docs/getting-started
examples/getting-started/installation.md - → https://jestjs.io/docs/using-matchers
examples/getting-started/using-matchers.md
测试:
- → https://jestjs.io/docs/asynchronous
examples/testing/asynchronous.md - → https://jestjs.io/docs/setup-teardown
examples/testing/setup-teardown.md - → https://jestjs.io/docs/mock-functions
examples/testing/mock-functions.md - → https://jestjs.io/docs/manual-mocks
examples/testing/manual-mocks.md - → https://jestjs.io/docs/snapshot-testing
examples/testing/snapshot-testing.md - → https://jestjs.io/docs/timer-mocks
examples/testing/timer-mocks.md - → https://jestjs.io/docs/es6-class-mocks
examples/testing/es6-class-mocks.md
配置:
- → https://jestjs.io/docs/configuration
examples/configuration/jest-config.md - → https://jestjs.io/docs/cli
examples/configuration/cli-options.md
API参考:
- → https://jestjs.io/docs/expect
api/expect.md - → https://jestjs.io/docs/mock-functions
api/mock-functions.md - → https://jestjs.io/docs/jest-object
api/jest-object.md - → https://jestjs.io/docs/api#testname-fn-timeout
api/test.md - → https://jestjs.io/docs/cli
api/cli.md
Examples and Templates
示例与模板
This skill includes detailed examples organized to match the official documentation structure. All examples are in the directory (see mapping above).
examples/To use examples:
- Identify the topic from the user's request
- Load the appropriate example file from the mapping above
- Follow the instructions, syntax, and best practices in that file
- Adapt the code examples to your specific use case
To use templates:
- Reference templates in directory for common scaffolding
templates/ - Adapt templates to your specific needs and coding style
本技能包含与官方文档结构一致的详细示例,所有示例均位于目录中(请参阅上述对应关系)。
examples/使用示例:
- 从用户的请求中确定主题
- 从上述对应关系中加载对应的示例文件
- 遵循文件中的说明、语法和最佳实践
- 根据具体用例调整代码示例
使用模板:
- 参考目录中的模板搭建通用结构
templates/ - 根据具体需求和编码风格调整模板
Best Practices
最佳实践
- Write descriptive test names: Use clear, descriptive test names
- Use appropriate matchers: Choose the right matcher for each assertion
- Mock external dependencies: Mock external APIs, databases, and services
- Test edge cases: Test both happy paths and error cases
- Keep tests isolated: Each test should be independent
- Use setup and teardown: Clean up resources properly
- Snapshot testing: Use snapshots for UI components, but review changes
- Coverage goals: Aim for meaningful coverage, not just high percentages
- Organize tests: Group related tests using describe blocks
- Async testing: Properly handle async code with async/await or promises
- 编写具有描述性的测试名称:使用清晰、明确的测试名称
- 使用合适的匹配器:为每个断言选择恰当的匹配器
- Mock外部依赖项:Mock外部API、数据库和服务
- 测试边界情况:同时测试正常流程和异常情况
- 保持测试独立性:每个测试应相互独立
- 使用初始化与清理函数:正确清理资源
- 快照测试:为UI组件使用快照测试,但需审查变更
- 覆盖率目标:追求有意义的覆盖率,而非单纯的高百分比
- 组织测试:使用describe块对相关测试进行分组
- 异步测试:使用async/await或Promise正确处理异步代码
Resources
资源
- Official Documentation: https://jestjs.io/
- Getting Started: https://jestjs.io/docs/getting-started
- API Reference: https://jestjs.io/docs/api
- GitHub Repository: https://github.com/jestjs/jest
- 官方文档: https://jestjs.io/
- 快速开始: https://jestjs.io/docs/getting-started
- API参考: https://jestjs.io/docs/api
- GitHub仓库: https://github.com/jestjs/jest
Keywords
关键词
Jest, testing, unit test, integration test, mock, spy, snapshot, matcher, assertion, async test, setup, teardown, coverage, CLI, configuration, TypeScript, React, Vue, Babel, 测试, 单元测试, 集成测试, Mock, 快照, 匹配器, 断言, 异步测试, 覆盖率, 配置
Jest, testing, unit test, integration test, mock, spy, snapshot, matcher, assertion, async test, setup, teardown, coverage, CLI, configuration, TypeScript, React, Vue, Babel, 测试, 单元测试, 集成测试, Mock, 快照, 匹配器, 断言, 异步测试, 覆盖率, 配置