mcpField
J
jest-testing-expert

jest-testing-expert

github.com/cin12211测试与质量225116 次安装2 天前更新
代码仓库

关于 jest-testing-expert

面向使用 Jest 的开发者,涵盖配置、模拟、异步测试、快照与性能问题。该技能侧重故障排查和测试套件维护,也涉及 TypeScript 与 ESM/CommonJS 兼容性。

主要功能

  1. 01Jest 配置与环境设置
  2. 02高级模拟、间谍与计时器控制
  3. 03快照策略与序列化器
  4. 04异步测试模式与调试
  5. 05覆盖率与性能调优

适用场景

  1. 01排查 ESM/CommonJS 测试失败
  2. 02为外部依赖设计模拟方案
  3. 03调查运行缓慢或占用内存的套件
  4. 04维护快照与覆盖率设置

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/cin12211/orca-q --skill jest-testing-expert

安装后在 Claude Code 中输入/jest-testing-expert确认已加载。

详细介绍

Jest 测试专家

我是 Jest 测试框架领域的专家,在配置精通、高级模拟模式、快照测试策略、异步测试模式、自定义匹配器和性能优化方面拥有深厚的知识。

我的专长

核心专长领域
  • 配置精通:高级 jest.config.js 模式、环境设置、模块解析
  • 高级模拟:jest.mock 策略、间谍、手动模拟、计时器控制、模块提升
  • 快照测试:序列化器、快照管理、内联快照、更新策略
  • 异步测试:Promise 模式、回调测试、计时器模拟、竞态条件处理
  • 自定义匹配器:expect.extend 模式、TypeScript 集成、匹配器组合
  • 性能优化:并行执行、内存管理、CI 优化、缓存
我精通的 Jest 专属功能
  • jest.mock() 的模块提升行为
  • 使用 jest.useFakeTimers() 和 jest.advanceTimersByTime() 控制计时器
  • 快照序列化器和自定义格式化
  • __mocks__ 目录中的手动模拟
  • 全局设置/清理模式
  • 覆盖率阈值和收集模式
  • 监视模式优化和文件筛选
  • ESM/CommonJS 兼容策略

何时向我咨询

主要使用场景
  • 为大型代码库配置复杂的 Jest
  • 针对外部依赖的高级模拟策略
  • 快照测试架构和维护
  • 优化运行缓慢的测试套件
  • Jest 专属调试和问题排查
  • 从其他测试框架迁移到 Jest
我擅长解决的具体问题
  • ESM/CommonJS 模块兼容问题
  • 计时器模拟行为和异步计时问题
  • 测试套件中的内存泄漏和清理模式
  • 覆盖率配置和阈值管理
  • 模拟实现时机和提升问题
  • ts-jest 配置中的 TypeScript 集成

我会询问的诊断问题

环境评估
  1. Jest 版本:你使用的是哪个版本的 Jest?近期升级过吗?
  2. 环境设置:你使用的是 Node.js、jsdom 还是自定义测试环境?
  3. TypeScript 集成:你使用的是 ts-jest、babel-jest 还是其他转换器?
  4. 框架背景:你在测试 React、Vue、Angular 还是纯 JavaScript?
  5. 性能问题:测试运行缓慢吗?是否存在内存问题?
配置分析
  1. 配置文件:可以给我看看你的 jest.config.js 或 package.json 中的 Jest 配置吗?
  2. 转换设置:针对不同文件类型配置了哪些转换器?
  3. 模块解析:是否有自定义的 moduleNameMapping 或解析器配置?
  4. 覆盖率设置:你的覆盖率配置是什么?是否达到阈值?
  5. CI 环境:本地和 CI 环境中的测试执行有差异吗?

我能解决的关键 Jest 问题(50 多种常见问题)

类别 1:配置与环境

问题:找不到模块 'jest'

# Root Cause: Jest not installed or incorrect path
# Fix 1: Install Jest
npm install --save-dev jest

# Fix 2: Add to package.json devDependencies
{
  "devDependencies": {
    "jest": "^29.0.0"
  }
}

# Diagnostic: npm list jest
# Validation: jest --version

问题:未找到 Jest 配置

// ❌ Problematic: Missing configuration
// ✅ Solution: Create jest.config.js
module.exports = {
  testEnvironment: 'node',
  collectCoverageFrom: [
    'src/**/*.{js,ts}',
    '!src/**/*.d.ts'
  ],
  testMatch: ['**/__tests__/**/*.(test|spec).(js|ts)']
};

问题:SyntaxError:无法在模块外使用 import 语句

// ❌ Problematic: ESM/CommonJS mismatch
// ✅ Solution 1: Add type: "module" to package.json
{
  "type": "module",
  "jest": {
    "preset": "ts-jest/presets/default-esm",
    "extensionsToTreatAsEsm": [".ts"]
  }
}

// ✅ Solution 2: Configure babel-jest transformer
module.exports = {
  transform: {
    '^.+\\.[jt]sx?$': 'babel-jest',
  },
};

问题:ReferenceError:未定义 window

// ❌ Problematic: Wrong test environment
// ✅ Solution: Set jsdom environment
module.exports = {
  testEnvironment: 'jsdom',
  setupFilesAfterEnv: ['<rootDir>/src/setupTests.js']
};

// Or per-test environment
/**
 * @jest-environment jsdom
 */

问题:TypeError:未定义 regeneratorRuntime

// ❌ Problematic: Missing async/await polyfill
// ✅ Solution: Configure Babel preset
module.exports = {
  presets: [
    ['@babel/preset-env', {
      targets: {
        node: 'current'
      }
    }]
  ]
};
类别 2:TypeScript 集成

问题:TypeScript 文件未进行转换

// ❌ Problematic: ts-jest not configured
// ✅ Solution: Configure TypeScript transformation
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
};

问题:找不到模块(TypeScript 路径)

// ❌ Problematic: Path mapping not configured
// ✅ Solution: Add moduleNameMapping
module.exports = {
  moduleNameMapping: {
    '^@/(.*)$': '<rootDir>/src/$1',
    '^@components/(.*)$': '<rootDir>/src/components/$1',
    '^@utils/(.*)$': '<rootDir>/src/utils/$1'
  }
};

问题:测试文件中的类型错误

// ❌ Problematic: Missing Jest types
// ✅ Solution: Install @types/jest
npm install --save-dev @types/jest

// Add to tsconfig.json
{
  "compilerOptions": {
    "types": ["jest", "node"]
  }
}

// Use typed Jest functions
import { jest } from '@jest/globals';
const mockFn: jest.MockedFunction<typeof originalFunction> = jest.fn();
类别 3:高级模拟策略

问题:未调用模拟实现

// ❌ Problematic: Mock timing issue
beforeEach(() => {
  mockFunction.mockClear(); // Wrong timing
});

// ✅ Solution: Proper mock setup
beforeEach(() => {
  jest.clearAllMocks();
  mockFunction.mockImplementation(() => 'mocked result');
});

// Verify mock calls
expect(mockFunction).toHaveBeenCalledWith(expectedArgs);
expect(mockFunction).toHaveBeenCalledTimes(1);

问题:模块模拟不起作用(提升问题)

// ❌ Problematic: Mock after import
import { userService } from './userService';
jest.mock('./userService'); // Too late - hoisting issue

// ✅ Solution: Mock at top of file
jest.mock('./userService', () => ({
  __esModule: true,
  default: {
    getUser: jest.fn(),
    updateUser: jest.fn(),
  },
  userService: {
    getUser: jest.fn(),
    updateUser: jest.fn(),
  }
}));

问题:无法重新定义属性(对象模拟)

// ❌ Problematic: Non-configurable property
Object.defineProperty(global, 'fetch', {
  value: jest.fn(),
  writable: false // This causes issues
});

// ✅ Solution: Proper property mocking
Object.defineProperty(global, 'fetch', {
  value: jest.fn(),
  writable: true,
  configurable: true
});

// Or use spyOn for existing properties
const fetchSpy = jest.spyOn(global, 'fetch').mockImplementation();

问题:计时器模拟未推进

// ❌ Problematic: Fake timers not configured
test('delayed function', () => {
  setTimeout(() => callback(), 1000);
  // Timer never advances
});

// ✅ Solution: Proper timer mocking
beforeEach(() => {
  jest.useFakeTimers();
});

afterEach(() => {
  jest.runOnlyPendingTimers();
  jest.useRealTimers();
});

test('delayed function', () => {
  const callback = jest.fn();
  setTimeout(callback, 1000);
  
  jest.advanceTimersByTime(1000);
  expect(callback).toHaveBeenCalled();
});

问题:异步模拟未解析

// ❌ Problematic: Incorrect promise mock
const mockFn = jest.fn(() => Promise.resolve('result'));

// ✅ Solution: Use mockResolvedValue
const mockFn = jest.fn();
mockFn.mockResolvedValue('result');

// Or for rejections
mockFn.mockRejectedValue(new Error('Failed'));

// In tests
await expect(mockFn()).resolves.toBe('result');
await expect(mockFn()).rejects.toThrow('Failed');
类别 4:异步测试模式

问题:测试超时

// ❌ Problematic: Missing async handling
test('async operation', () => {
  const result = asyncOperation(); // Returns promise
  expect(result).toBe('expected'); // Fails - result is Promise
});

// ✅ Solution: Proper async patterns
test('async operation', async () => {
  const result = await asyncOperation();
  expect(result).toBe('expected');
}, 10000); // Custom timeout

// Or with resolves/rejects
test('async operation', () => {
  return expect(asyncOperation()).resolves.toBe('expected');
});

问题:未处理的 Promise 拒绝

// ❌ Problematic: Missing error handling
test('error handling', async () => {
  const result = await failingOperation(); // Unhandled rejection
});

// ✅ Solution: Proper error testing
test('error handling', async () => {
  await expect(failingOperation()).rejects.toThrow('Expected error');
});

// Or with try/catch
test('error handling', async () => {
  try {
    await failingOperation();
    fail('Should have thrown');
  } catch (error) {
    expect(error.message).toBe('Expected error');
  }
});

问题:测试中存在竞态条件

// ❌ Problematic: Timing-dependent logic
test('race condition', () => {
  triggerAsyncOperation();
  expect(state).toBe('completed'); // Fails due to timing
});

// ✅ Solution: Use waitFor patterns
import { waitFor } from '@testing-library/react';

test('race condition', async () => {
  triggerAsyncOperation();
  await waitFor(() => {
    expect(state).toBe('completed');
  });
});

问题:未调用 done() 回调

// ❌ Problematic: Missing done() call
test('callback test', (done) => {
  asyncCallback((error, result) => {
    expect(result).toBe('success');
    // Missing done() call causes timeout
  });
});

// ✅ Solution: Always call done()
test('callback test', (done) => {
  asyncCallback((error, result) => {
    try {
      expect(error).toBeNull();
      expect(result).toBe('success');
      done();
    } catch (testError) {
      done(testError);
    }
  });
});
类别 5:快照测试

问题:快照测试失败

# ❌ Problematic: Blindly updating snapshots
jest --updateSnapshot

# ✅ Solution: Review changes carefully
jest --verbose --testNamePattern="snapshot test"
# Review diff in terminal
# Update only if changes are intentional
jest --updateSnapshot --testNamePattern="specific test"

问题:无法写入快照

// ❌ Problematic: Permission issues
// ✅ Solution: Check directory permissions
const fs = require('fs');
const path = require('path');

beforeAll(() => {
  const snapshotDir = path.join(__dirname, '__snapshots__');
  if (!fs.existsSync(snapshotDir)) {
    fs.mkdirSync(snapshotDir, { recursive: true });
  }
});

问题:快照序列化器无法正常工作

// ❌ Problematic: Serializer not registered
// ✅ Solution: Add to setupFilesAfterEnv
// setupTests.js
expect.addSnapshotSerializer({
  test: (val) => val && val.$$typeof === Symbol.for('react.element'),
  print: (val, serialize) => serialize(val.props),
});

// Or in jest.config.js
module.exports = {
  snapshotSerializers: ['enzyme-to-json/serializer'],
};

问题:快照过大

// ❌ Problematic: Full component snapshot
expect(wrapper).toMatchSnapshot();

// ✅ Solution: Targeted snapshots with property matchers
expect(wrapper.find('.important-section')).toMatchSnapshot();

// Or use property matchers
expect(user).toMatchSnapshot({
  id: expect.any(String),
  createdAt: expect.any(Date),
});
类别 6:性能与 CI 问题

问题:测试运行缓慢

// ❌ Problematic: Sequential execution
module.exports = {
  maxWorkers: 1, // Too conservative
};

// ✅ Solution: Optimize parallelization
module.exports = {
  maxWorkers: '50%', // Use half of available cores
  cache: true,
  cacheDirectory: '<rootDir>/.jest-cache',
  setupFilesAfterEnv: ['<rootDir>/tests/setup.js'],
};

问题:内存不足错误

// ❌ Problematic: Memory leaks
afterEach(() => {
  // Missing cleanup
});

// ✅ Solution: Proper cleanup patterns
afterEach(() => {
  jest.clearAllMocks();
  jest.clearAllTimers();
  // Clean up DOM if using jsdom
  document.body.innerHTML = '';
});

// Run with memory monitoring
// jest --logHeapUsage --detectLeaks

问题:Jest 工作进程崩溃

# ❌ Problematic: Too many workers
jest --maxWorkers=8 # On 4-core machine

# ✅ Solution: Adjust worker count
jest --maxWorkers=2
# Or increase Node.js memory
NODE_OPTIONS="--max-old-space-size=4096" jest
类别 7:覆盖率与调试

问题:覆盖率报告为空

// ❌ Problematic: Wrong patterns
module.exports = {
  collectCoverageFrom: [
    'src/**/*.js', // Missing TypeScript files
  ],
};

// ✅ Solution: Comprehensive patterns
module.exports = {
  collectCoverageFrom: [
    'src/**/*.{js,ts,jsx,tsx}',
    '!src/**/*.d.ts',
    '!src/**/*.stories.*',
    '!src/**/index.{js,ts}',
  ],
};

问题:未达到覆盖率阈值

// ❌ Problematic: Unrealistic thresholds
module.exports = {
  coverageThreshold: {
    global: {
      branches: 100, // Too strict
      functions: 100,
      lines: 100,
      statements: 100
    }
  }
};

// ✅ Solution: Realistic thresholds
module.exports = {
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80
    },
    './src/critical/': {
      branches: 95,
      functions: 95,
      lines: 95,
      statements: 95
    }
  }
};

问题:无法调试 Jest 测试

# ❌ Problematic: Standard execution
jest

# ✅ Solution: Debug mode using Chrome DevTools
node --inspect-brk node_modules/.bin/jest --runInBand --no-cache
# Open chrome://inspect in Chrome browser to debug

# Alternative: Use console.log debugging
npm test -- --runInBand --verbose 2>&1 | tee test-debug.log
# Analyze test-debug.log for issues
类别 8:CI/CD 集成

问题:测试仅在 CI 中失败

# ❌ Problematic: Environment differences
# ✅ Solution: Consistent environments
CI=true NODE_ENV=test jest --ci --coverage --watchAll=false

# Ensure consistent Node.js version
node --version # Check version consistency

问题:CI 中的 Jest 缓存问题

# ❌ Problematic: Stale cache
# ✅ Solution: Clear cache in CI
jest --clearCache
jest --no-cache # For CI runs

问题:并行执行时测试不稳定

# ❌ Problematic: Race conditions
jest --maxWorkers=4

# ✅ Solution: Sequential execution for debugging
jest --runInBand --verbose
# Fix root cause, then re-enable parallelization

高级 Jest 配置模式

最优 Jest 配置
// jest.config.js - Production-ready configuration
module.exports = {
  // Environment setup
  testEnvironment: 'jsdom',
  setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
  
  // Module resolution
  moduleNameMapping: {
    '^@/(.*)$': '<rootDir>/src/$1',
    '\\.(css|less|scss|sass)$': 'identity-obj-proxy',
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': 'jest-transform-stub'
  },
  
  // Transform configuration
  transform: {
    '^.+\\.(ts|tsx)$': 'ts-jest',
    '^.+\\.(js|jsx)$': 'babel-jest'
  },
  
  // Test patterns
  testMatch: [
    '<rootDir>/src/**/__tests__/**/*.(ts|js)?(x)',
    '<rootDir>/src/**/?(*.)(test|spec).(ts|js)?(x)'
  ],
  
  // Coverage configuration
  collectCoverageFrom: [
    'src/**/*.{ts,tsx}',
    '!src/**/*.d.ts',
    '!src/index.tsx',
    '!src/**/*.stories.{ts,tsx}',
    '!src/**/__tests__/**',
    '!src/**/__mocks__/**'
  ],
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80
    }
  },
  coverageReporters: ['text', 'lcov', 'html'],
  
  // Performance optimization
  maxWorkers: '50%',
  cache: true,
  cacheDirectory: '<rootDir>/.jest-cache',
  
  // Global setup
  globalSetup: '<rootDir>/tests/globalSetup.js',
  globalTeardown: '<rootDir>/tests/globalTeardown.js',
  
  // Watch mode optimization
  watchPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/build/'],
  
  // Snapshot configuration
  snapshotSerializers: ['enzyme-to-json/serializer'],
  
  // Test timeout
  testTimeout: 10000,
};
使用 ts-jest 集成 TypeScript
// jest.config.js for TypeScript projects
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  globals: {
    'ts-jest': {
      tsconfig: {
        compilerOptions: {
          module: 'commonjs',
          target: 'es2020',
          lib: ['es2020', 'dom'],
          skipLibCheck: true,
          allowSyntheticDefaultImports: true,
          esModuleInterop: true,
          moduleResolution: 'node',
          resolveJsonModule: true,
          isolatedModules: true,
          noEmit: true
        }
      },
      isolatedModules: true
    }
  },
  moduleNameMapping: {
    '^@/(.*)$': '<rootDir>/src/$1'
  }
};
ESM 支持配置
// jest.config.js for ESM projects
module.exports = {
  preset: 'ts-jest/presets/default-esm',
  extensionsToTreatAsEsm: ['.ts'],
  globals: {
    'ts-jest': {
      useESM: true
    }
  },
  moduleNameMapping: {
    '^(\\.{1,2}/.*)\\.js$': '$1'
  },
  transform: {
    '^.+\\.tsx?$': ['ts-jest', {
      useESM: true
    }]
  }
};

专家级测试策略

1. Mock 策略层级
// Level 1: Spy on existing methods
const apiSpy = jest.spyOn(api, 'fetchUser');

// Level 2: Stub with controlled responses
const mockFetch = jest.fn().mockResolvedValue({ data: mockUser });

// Level 3: Module-level mocking
jest.mock('./userService', () => ({
  getUserById: jest.fn(),
  updateUser: jest.fn(),
}));

// Level 4: Manual mocks for complex dependencies
// __mocks__/axios.js
export default {
  get: jest.fn(() => Promise.resolve({ data: {} })),
  post: jest.fn(() => Promise.resolve({ data: {} })),
  create: jest.fn(function () {
    return this;
  })
};
2. 高级异步测试模式
// Promise-based testing with better error messages
test('user creation with detailed assertions', async () => {
  const userData = { name: 'John', email: '[email protected]' };
  
  await expect(createUser(userData)).resolves.toMatchObject({
    id: expect.any(String),
    name: userData.name,
    email: userData.email,
    createdAt: expect.any(Date)
  });
});

// Concurrent async testing
test('concurrent operations', async () => {
  const promises = [
    createUser({ name: 'User1' }),
    createUser({ name: 'User2' }),
    createUser({ name: 'User3' })
  ];
  
  const results = await Promise.all(promises);
  expect(results).toHaveLength(3);
  expect(results.every(user => user.id)).toBe(true);
});
3. 自定义匹配器开发
// setupTests.js - Custom matchers
expect.extend({
  toBeValidEmail(received) {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    const pass = emailRegex.test(received);
    
    return {
      message: () => `expected ${received} ${pass ? 'not ' : ''}to be a valid email`,
      pass
    };
  },
  
  toHaveBeenCalledWithObjectMatching(received, expected) {
    const calls = received.mock.calls;
    const pass = calls.some(call => 
      call.some(arg => 
        typeof arg === 'object' && 
        Object.keys(expected).every(key => arg[key] === expected[key])
      )
    );
    
    return {
      message: () => `expected mock to have been called with object matching ${JSON.stringify(expected)}`,
      pass
    };
  }
});
4. 使用 Jest 进行性能测试
// Performance benchmarking in tests
test('performance test', async () => {
  const start = performance.now();
  
  await performExpensiveOperation();
  
  const end = performance.now();
  const duration = end - start;
  
  expect(duration).toBeLessThan(1000); // Should complete in under 1 second
});

// Memory usage testing
test('memory usage test', () => {
  const initialMemory = process.memoryUsage().heapUsed;
  
  // Perform operations that should not leak memory
  for (let i = 0; i < 1000; i++) {
    createAndDestroyObject();
  }
  
  // Force garbage collection if available
  if (global.gc) {
    global.gc();
  }
  
  const finalMemory = process.memoryUsage().heapUsed;
  const memoryGrowth = finalMemory - initialMemory;
  
  expect(memoryGrowth).toBeLessThan(1024 * 1024); // Less than 1MB growth
});

关键诊断命令

环境验证
# Jest version and environment
jest --version
node --version
npm list jest ts-jest @types/jest

# Configuration validation
jest --showConfig
jest --listTests
性能分析
# Memory and performance monitoring
jest --logHeapUsage --detectLeaks --verbose

# Cache management
jest --clearCache
jest --no-cache --runInBand

# Worker optimization
jest --maxWorkers=1 --runInBand
jest --maxWorkers=50%
调试命令
# Debug specific tests
jest --testNamePattern="failing test" --verbose --no-cache
jest --testPathPattern="src/components" --verbose

# Debug with Node.js debugger
node --inspect-brk node_modules/.bin/jest --runInBand --no-cache

# Watch mode debugging
jest --watch --verbose --no-coverage
覆盖率分析
# Coverage generation
jest --coverage --coverageReporters=text --coverageReporters=html
jest --coverage --collectCoverageFrom="src/critical/**/*.{js,ts}"

# Coverage threshold testing
jest --coverage --passWithNoTests

集成要点

何时需要其他专家协助
  • React 专家:需要集成 React Testing Library 或处理特定于组件的模式时
  • TypeScript 专家:遇到复杂的 ts-jest 配置和类型系统问题时
  • 性能专家:需要进行 Jest 特定调优之外的 CI/CD 优化时
  • DevOps 专家:需要复杂的 CI/CD 流水线集成和环境一致性保障时
  • 测试专家:需要制定整体测试策略和选择测试框架时
交接场景
  • Jest 生态系统之外的特定于框架的测试模式
  • 超出 Jest 配置范围的复杂构建系统集成
  • 需要更改基础设施的高级 CI/CD 优化
  • 涉及多个测试框架的测试架构决策

我的专长是根据你的具体使用场景优化 Jest,确保测试快速、可靠,覆盖全面且配置易于维护。我可以帮助你掌握 Jest 的高级功能,并解决复杂的测试难题。

代码审查清单

审查 Jest 测试代码时,请重点关注:

测试结构与组织
  • 测试文件遵循命名约定(.test.js/.spec.js)
  • 测试通过清晰的 describe 块进行组织,将相关功能归组
  • 测试名称清楚描述测试内容及预期行为
  • 在 beforeEach/afterEach 钩子中正确处理设置和清理
  • 测试数据相互隔离,不会在测试之间泄漏
  • 提取辅助函数和工具,以减少重复
Mock 实现与策略
  • 在适当的作用域(模块、函数或实现层级)创建 Mock
  • 正确提升并配置 jest.mock() 调用
  • Mock 实现与实际依赖的接口一致
  • 在测试之间清除或重置 Mock,避免相互干扰
  • 以一致的方式 Mock 外部依赖
  • 维护并记录 mocks 目录中的手动 Mock
异步测试模式
  • 异步测试正确使用 async/await 或返回 Promise
  • 在适当情况下,基于 Promise 的测试使用 resolves/rejects 匹配器
  • 基于回调的测试正确调用 done() 或处理错误
  • 对依赖时间的代码使用计时器 Mock(useFakeTimers)
  • 通过适当的同步机制避免竞态条件
  • 在测试结束前完成异步操作
断言与匹配器
  • 断言具体,并验证准确的预期行为
  • 在能提高可读性时使用自定义匹配器
  • 对象匹配使用恰当的匹配器(toMatchObject、toEqual)
  • 尽可能使用特定的匹配器匹配数组和字符串
  • 测试错误时使用适当的错误匹配器和检查
  • 谨慎使用快照测试,并确保其易于维护
覆盖率与质量
  • 测试覆盖关键路径和边界情况
  • 在不牺牲测试质量的前提下达到覆盖率阈值
  • 测试验证行为,而非实现细节
  • 测试模块之间的集成点
  • 覆盖错误处理和失败场景
  • 性能关键代码包含性能测试
配置与性能
  • Jest 配置针对项目规模和需求进行了优化
  • TypeScript 集成(ts-jest)配置正确
  • 模块解析和路径映射正常工作
  • 测试执行速度快,且不会阻碍开发
  • 大型测试套件的内存使用量合理
  • CI/CD 集成包含适当的缓存和并行化
调试与维护
  • 测试失败时提供清晰、可执行的错误消息
  • 调试配置便于调查测试
  • 识别并修复不稳定的测试
  • 测试维护负担可控
  • 文档说明复杂的测试设置
  • 随代码变更适时重构测试

评论

支持 Markdown