testing-expert

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Testing Expert Skill

测试专家技能

Expert in testing strategies for React, Next.js, and NestJS applications.
专注于React、Next.js和NestJS应用的测试策略。

When to Use This Skill

何时使用此技能

  • Writing unit tests
  • Creating integration tests
  • Setting up E2E tests
  • Testing React components
  • Testing API endpoints
  • Testing database operations
  • Setting up test infrastructure
  • Reviewing test coverage
  • 编写单元测试
  • 创建集成测试
  • 搭建E2E测试
  • 测试React组件
  • 测试API端点
  • 测试数据库操作
  • 搭建测试基础设施
  • 审查测试覆盖率

Project Context Discovery

项目上下文探查

  1. Scan Documentation: Check
    .agents/SYSTEM/ARCHITECTURE.md
    for testing architecture
  2. Identify Tools: Jest/Vitest, React Testing Library, Supertest, Playwright/Cypress
  3. Discover Patterns: Review existing test files, utilities, mocking patterns
  4. Use Project-Specific Skills: Check for
    [project]-testing-expert
    skill
  1. 扫描文档: 查看
    .agents/SYSTEM/ARCHITECTURE.md
    了解测试架构
  2. 识别工具: Jest/Vitest、React Testing Library、Supertest、Playwright/Cypress
  3. 发现模式: 审查现有测试文件、工具函数、模拟模式
  4. 使用项目特定技能: 查看是否存在
    [project]-testing-expert
    技能

Core Testing Principles

核心测试原则

Testing Pyramid

测试金字塔

  • Unit Tests (70%): Fast, isolated, test individual functions/components
  • Integration Tests (20%): Test component interactions
  • E2E Tests (10%): Test full user flows
  • 单元测试(70%):快速、隔离,测试单个函数/组件
  • 集成测试(20%):测试组件间交互
  • E2E测试(10%):测试完整用户流程

Coverage Targets

覆盖率目标

  • Line coverage: > 80%
  • Branch coverage: > 75%
  • Function coverage: > 85%
  • Critical paths: 100%
  • 行覆盖率:> 80%
  • 分支覆盖率:> 75%
  • 函数覆盖率:> 85%
  • 关键路径:100%

Test Organization

测试组织

src/
  users/
    users.controller.ts
    users.controller.spec.ts  # Unit tests
    users.service.ts
    users.service.spec.ts
  __tests__/
    integration/
    e2e/
src/
  users/
    users.controller.ts
    users.controller.spec.ts  # 单元测试
    users.service.ts
    users.service.spec.ts
  __tests__/
    integration/
    e2e/

Test Quality (AAA Pattern)

测试质量(AAA模式)

typescript
it('should return users filtered by organization', async () => {
  // Arrange: Set up test data
  const organizationId = 'org1';
  const expectedUsers = [{ organization: organizationId }];

  // Act: Execute the code being tested
  const result = await service.findAll(organizationId);

  // Assert: Verify the result
  expect(result).toEqual(expectedUsers);
});
typescript
it('should return users filtered by organization', async () => {
  // Arrange: 设置测试数据
  const organizationId = 'org1';
  const expectedUsers = [{ organization: organizationId }];

  // Act: 执行待测试代码
  const result = await service.findAll(organizationId);

  // Assert: 验证结果
  expect(result).toEqual(expectedUsers);
});

Good Tests Are

优质测试的特征

  • Independent (no test dependencies)
  • Fast (< 100ms each)
  • Repeatable (same result every time)
  • Meaningful (test real behavior)
  • Maintainable (easy to update)
  • 独立性(无测试依赖)
  • 快速(每个测试<100ms)
  • 可重复(每次结果一致)
  • 有意义(测试真实行为)
  • 可维护(易于更新)

Testing Best Practices Summary

测试最佳实践总结

  1. Test Isolation: Each test independent, clean up after
  2. Meaningful Tests: Test behavior, not implementation
  3. Mocking Strategy: Mock external dependencies, not what you're testing
  4. Test Data: Use factories, keep data minimal, clean up
  5. Coverage: High coverage, focus on critical paths
  1. 测试隔离: 每个测试独立,测试后清理环境
  2. 有意义的测试: 测试行为而非实现细节
  3. 模拟策略: 模拟外部依赖,而非待测试对象
  4. 测试数据: 使用工厂函数,保持数据精简,测试后清理
  5. 覆盖率: 追求高覆盖率,重点关注关键路径

Integration

集成方案

Test TypeToolsUse Case
UnitJest/VitestFunctions, components, services
IntegrationSupertest + JestController + Service + DB
E2EPlaywright/CypressFull user flows
ComponentReact Testing LibraryReact component behavior

For complete React Testing Library examples, hook testing, Next.js page/API testing, NestJS service/controller testing, integration test setup, E2E test patterns, MongoDB testing, authentication helpers, test fixtures, and mocking patterns, see:
references/full-guide.md
测试类型工具适用场景
单元测试Jest/Vitest函数、组件、服务
集成测试Supertest + Jest控制器+服务+数据库
E2E测试Playwright/Cypress完整用户流程
组件测试React Testing LibraryReact组件行为

如需完整的React Testing Library示例、Hook测试、Next.js页面/API测试、NestJS服务/控制器测试、集成测试搭建、E2E测试模式、MongoDB测试、认证辅助工具、测试夹具及模拟模式,请查看:
references/full-guide.md