jest

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

When 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:
  1. Identify the topic from the user's request:
    • Getting started/快速开始 →
      examples/getting-started/installation.md
      or
      examples/getting-started/using-matchers.md
    • Testing/测试 →
      examples/testing/asynchronous.md
      or
      examples/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
  2. Load the appropriate example file from the
    examples/
    directory:
    Getting Started (快速开始) -
    examples/getting-started/
    :
    • examples/getting-started/installation.md
      - Installing Jest and basic setup
    • examples/getting-started/using-matchers.md
      - Using Jest matchers and assertions
    Testing (测试) -
    examples/testing/
    :
    • examples/testing/asynchronous.md
      - Testing asynchronous code
    • examples/testing/setup-teardown.md
      - Setup and teardown functions
    • examples/testing/mock-functions.md
      - Mocking functions
    • examples/testing/manual-mocks.md
      - Manual mocks
    • examples/testing/snapshot-testing.md
      - Snapshot testing
    • examples/testing/timer-mocks.md
      - Mocking timers
    • examples/testing/es6-class-mocks.md
      - Mocking ES6 classes
    Configuration (配置) -
    examples/configuration/
    :
    • examples/configuration/jest-config.md
      - Jest configuration options
    • examples/configuration/cli-options.md
      - CLI options and flags
    • examples/configuration/babel.md
      - Using Jest with Babel
    • examples/configuration/typescript.md
      - Using Jest with TypeScript
    Guides (指南) -
    examples/guides/
    :
    • examples/guides/migrating-to-jest.md
      - Migrating to Jest
    • examples/guides/testing-react-apps.md
      - Testing React applications
    • examples/guides/testing-vue-apps.md
      - Testing Vue applications
  3. Follow the specific instructions in that example file for syntax, structure, and best practices
    Important 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
  4. Reference API documentation in the
    api/
    directory when needed:
    • api/expect.md
      - expect() API and matchers
    • api/mock-functions.md
      - Mock functions API
    • api/jest-object.md
      - Jest object API
    • api/test.md
      - test() and describe() API
    • api/cli.md
      - CLI commands and options
  5. Use templates from the
    templates/
    directory:
    • templates/jest-config.md
      - Jest configuration templates
    • templates/test-template.md
      - Test file templates
    • templates/setup-file.md
      - Setup file templates
  1. 从用户的请求中确定主题
    • 快速开始 →
      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
  2. 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
      - Mock函数
    • examples/testing/manual-mocks.md
      - 手动Mock
    • examples/testing/snapshot-testing.md
      - 快照测试
    • examples/testing/timer-mocks.md
      - Mock计时器
    • examples/testing/es6-class-mocks.md
      - Mock ES6类
    配置 -
    examples/configuration/
    :
    • examples/configuration/jest-config.md
      - Jest配置选项
    • examples/configuration/cli-options.md
      - CLI选项与参数
    • examples/configuration/babel.md
      - 在Babel中使用Jest
    • examples/configuration/typescript.md
      - 在TypeScript中使用Jest
    指南 -
    examples/guides/
    :
    • examples/guides/migrating-to-jest.md
      - 迁移至Jest
    • examples/guides/testing-react-apps.md
      - React应用测试
    • examples/guides/testing-vue-apps.md
      - Vue应用测试
  3. 遵循示例文件中的具体说明,包括语法、结构和最佳实践
    重要说明:
    • 所有示例均遵循Jest最佳实践
    • 适用的示例同时包含JavaScript和TypeScript版本
    • 每个示例文件都包含核心概念、代码示例和关键点
    • 请务必查看示例文件以了解最佳实践和常见模式
  4. 必要时参考
    api/
    目录中的API文档
    :
    • api/expect.md
      - expect() API与匹配器
    • api/mock-functions.md
      - Mock函数API
    • api/jest-object.md
      - Jest对象API
    • api/test.md
      - test()与describe() API
    • api/cli.md
      - CLI命令与选项
  5. 使用
    templates/
    目录中的模板
    :
    • templates/jest-config.md
      - Jest配置模板
    • templates/test-template.md
      - 测试文件模板
    • templates/setup-file.md
      - 初始化文件模板
Getting Started (快速开始):
Testing (测试):
Configuration (配置):
API Reference:
快速开始:
测试:
配置:
API参考:

Examples and Templates

示例与模板

This skill includes detailed examples organized to match the official documentation structure. All examples are in the
examples/
directory (see mapping above).
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
    templates/
    directory for common scaffolding
  • Adapt templates to your specific needs and coding style
本技能包含与官方文档结构一致的详细示例,所有示例均位于
examples/
目录中(请参阅上述对应关系)。
使用示例:
  • 从用户的请求中确定主题
  • 从上述对应关系中加载对应的示例文件
  • 遵循文件中的说明、语法和最佳实践
  • 根据具体用例调整代码示例
使用模板:
  • 参考
    templates/
    目录中的模板搭建通用结构
  • 根据具体需求和编码风格调整模板

Best Practices

最佳实践

  1. Write descriptive test names: Use clear, descriptive test names
  2. Use appropriate matchers: Choose the right matcher for each assertion
  3. Mock external dependencies: Mock external APIs, databases, and services
  4. Test edge cases: Test both happy paths and error cases
  5. Keep tests isolated: Each test should be independent
  6. Use setup and teardown: Clean up resources properly
  7. Snapshot testing: Use snapshots for UI components, but review changes
  8. Coverage goals: Aim for meaningful coverage, not just high percentages
  9. Organize tests: Group related tests using describe blocks
  10. Async testing: Properly handle async code with async/await or promises
  1. 编写具有描述性的测试名称:使用清晰、明确的测试名称
  2. 使用合适的匹配器:为每个断言选择恰当的匹配器
  3. Mock外部依赖项:Mock外部API、数据库和服务
  4. 测试边界情况:同时测试正常流程和异常情况
  5. 保持测试独立性:每个测试应相互独立
  6. 使用初始化与清理函数:正确清理资源
  7. 快照测试:为UI组件使用快照测试,但需审查变更
  8. 覆盖率目标:追求有意义的覆盖率,而非单纯的高百分比
  9. 组织测试:使用describe块对相关测试进行分组
  10. 异步测试:使用async/await或Promise正确处理异步代码

Resources

资源

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, 快照, 匹配器, 断言, 异步测试, 覆盖率, 配置