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. 01ESM/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. モック戦略の階層
// 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 フックでセットアップと後処理が適切に行われている
  • テストデータが分離され、テスト間に漏れない
  • 重複を減らすため、ヘルパー関数とユーティリティが切り出されている
モックの実装と戦略
  • モックが適切なスコープ(モジュール、関数、または実装レベル)で作成されている
  • jest.mock() の呼び出しが適切に巻き上げられ、設定されている
  • モックの実装が実際の依存関係のインターフェースと一致している
  • テスト間の干渉を防ぐため、モックがクリアまたはリセットされている
  • 外部依存関係が一貫してモックされている
  • mocks ディレクトリ内の手動モックが保守され、文書化されている
非同期テストのパターン
  • 非同期テストで async/await を使用するか、Promise を適切に返している
  • Promise ベースのテストで、適切な場合に resolves/rejects マッチャーを使用している
  • コールバックベースのテストで done() を適切に呼び出すか、エラーを処理している
  • 時間に依存するコードにタイマーモック(useFakeTimers)を使用している
  • 適切な同期によって競合状態を回避している
  • テストの終了前に非同期処理が完了している
アサーションとマッチャー
  • アサーションが具体的で、期待される動作を正確にテストしている
  • 可読性が向上する場合は、カスタムマッチャーを使用している
  • オブジェクトのマッチングに適切なマッチャー(toMatchObject、toEqual)を使用している
  • 配列や文字列のマッチングには、可能な限り専用のマッチャーを使用している
  • エラーのテストに適切なエラーマッチャーとチェックを使用している
  • スナップショットテストを慎重に使用し、保守しやすい状態に保っている
カバレッジと品質
  • テストが重要なパスとエッジケースを網羅している
  • テストの品質を犠牲にすることなく、カバレッジのしきい値を満たしている
  • テストが実装の詳細ではなく、動作を検証している
  • モジュール間の統合ポイントをテストしている
  • エラー処理と失敗のシナリオを網羅している
  • パフォーマンスが重要なコードにパフォーマンステストを含めている
設定とパフォーマンス
  • Jest の設定を、プロジェクトの規模と要件に合わせて最適化している
  • TypeScript との統合(ts-jest)を適切に設定している
  • モジュール解決とパスマッピングが正しく機能している
  • テストの実行が速く、開発を妨げない
  • 大規模なテストスイートでもメモリ使用量が妥当である
  • CI/CD との統合に適切なキャッシュと並列化を含めている
デバッグと保守
  • テストの失敗時に、明確で対処可能なエラーメッセージが表示される
  • デバッグ設定により、テストを簡単に調査できる
  • 不安定なテストを特定し、修正している
  • テストの保守負担が管理可能である
  • 複雑なテスト設定についてドキュメントで説明している
  • コード変更に応じて、テストのリファクタリングを適切に行っている

コメント