jpskill.com
🛠️ 開発・MCP コミュニティ

typescript-unit-testing

TypeScript/NestJSプロジェクトで、JestやDeepMockedなどを活用し、AAA構成に基づいたユニットテストを効率的に実装・実行・改善し、テスト品質やカバレッジ向上、デバッグを支援するSkill。

📜 元の英語説明(参考)

Unit testing for TypeScript/NestJS projects using Jest, @golevelup/ts-jest (DeepMocked/createMock), and in-memory databases, with AAA structure. Use whenever the user is working on `.spec.ts` files or asks to set up Jest, write/add tests for a service/usecase/controller/guard/interceptor/pipe/filter, mock dependencies, review test quality or coverage, run unit tests, debug failing or flaky tests, or optimize test performance and open handles.

🇯🇵 日本人クリエイター向け解説

一言でいうと

TypeScript/NestJSプロジェクトで、JestやDeepMockedなどを活用し、AAA構成に基づいたユニットテストを効率的に実装・実行・改善し、テスト品質やカバレッジ向上、デバッグを支援するSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o typescript-unit-testing.zip https://jpskill.com/download/23729.zip && unzip -o typescript-unit-testing.zip && rm typescript-unit-testing.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/23729.zip -OutFile "$d\typescript-unit-testing.zip"; Expand-Archive "$d\typescript-unit-testing.zip" -DestinationPath $d -Force; ri "$d\typescript-unit-testing.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して typescript-unit-testing.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → typescript-unit-testing フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
20

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

単体テストスキル

単体テストは、すべての外部依存関係をモックすることで、個々の関数、メソッド、およびクラスを分離して検証します。


ワークフロー

単体テストタスクを段階的に実行するには、適切なワークフローを使用してください。

ワークフロー 目的 使用するタイミング
セットアップ テストインフラストラクチャの初期化 新規プロジェクトまたはテスト設定が不足している場合
作成 新しい単体テストの作成 コンポーネントのテストを作成する場合
レビュー 既存のテストのレビュー コードレビュー、品質監査
実行 テストの実行 テストの実行、結果の分析
デバッグ 失敗したテストの修正 テストが失敗し、診断が必要な場合
最適化 テストパフォーマンスの改善 テストが遅い、保守性

ワークフロー選択ガイド

重要: テストタスクを開始する前に、ユーザーの意図を特定し、適切なワークフローをロードしてください。

ユーザーの意図を検出 → ワークフローを選択

ユーザーの発言 / 要望 ロードするワークフロー ファイル
「テストを設定する」、「Jestを設定する」、「プロジェクトにテストを追加する」、「テスト依存関係をインストールする」 セットアップ workflows/setup/workflow.md
「テストを書く」、「テストを追加する」、「テストを作成する」、「このサービス/コントローラーをテストする」 作成 workflows/writing/workflow.md
「テストをレビューする」、「テストの品質を確認する」、「テストを監査する」、「これらのテストは良いか?」 レビュー workflows/reviewing/workflow.md
「テストを実行する」、「テストを実行する」、「テストがパスするか確認する」、「テスト結果を表示する」 実行 workflows/running/workflow.md
「テストを修正する」、「テストをデバッグする」、「テストが失敗している」、「なぜこのテストは壊れているのか?」 デバッグ workflows/debugging/workflow.md
「テストを高速化する」、「テストを最適化する」、「テストが遅い」、「オープンハンドルを修正する」 最適化 workflows/optimizing/workflow.md

ワークフロー実行プロトコル

  1. 常に最初にワークフローファイルをロードする - 行動を起こす前にワークフロー全体を読んでください。
  2. 各ステップを順番に実行する - 次に進む前にチェックポイントを完了してください。
  3. 指示された知識ファイルをロードする - 各ワークフローは、どの references/ ファイルを読むべきかを指定しています。
  4. 完了後にコンプライアンスを検証する - 関連する参照ファイルを再読して品質を確保してください。

知識ベースの構造

references/
├── common/              # コアとなるテストの基本
│   ├── knowledge.md     # テストの哲学とテストピラミッド
│   ├── rules.md         # 必須のテストルール (AAA、命名、カバレッジ)
│   ├── assertions.md    # アサーションパターンとマッチャー
│   ├── examples.md      # カテゴリ別の包括的な例
│   ├── detect-open-handles.md   # オープンハンドルの検出とクリーンアップ
│   └── performance-optimization.md  # Jestランタイムの最適化
│
├── nestjs/              # NestJSコンポーネントのテスト
│   ├── services.md      # サービス/ユースケースのテストパターン
│   ├── controllers.md   # コントローラーのテストパターン
│   ├── guards.md        # ガードのテストパターン
│   ├── interceptors.md  # インターセプターのテストパターン
│   └── pipes-filters.md # パイプとフィルターのテスト
│
├── mocking/             # モックパターンと戦略
│   ├── deep-mocked.md   # @golevelup/ts-jest パターン
│   ├── jest-native.md   # Jest.fn、spyOn、mock パターン
│   └── factories.md     # テストデータファクトリパターン
│
├── repository/          # リポジトリのテスト
│   ├── mongodb.md       # mongodb-memory-server パターン
│   └── postgres.md      # pg-mem パターン
│
├── kafka/               # NestJS Kafkaマイクロサービスのテスト
│   └── kafka.md         # ClientKafka、@MessagePattern、@EventPattern ハンドラー
│
└── redis/               # Redisキャッシュのテスト
    └── redis.md         # キャッシュ操作、ヘルスチェック、グレースフルデグラデーション

タスク別クイックリファレンス

単体テストの作成

  1. 必須: references/common/rules.md を読む - AAAパターン、命名、カバレッジ
  2. references/common/assertions.md を読む - アサーションのベストプラクティス
  3. コンポーネント固有のファイルを読む:
    • サービス: references/nestjs/services.md
    • コントローラー: references/nestjs/controllers.md
    • ガード: references/nestjs/guards.md
    • インターセプター: references/nestjs/interceptors.md
    • パイプ/フィルター: references/nestjs/pipes-filters.md

モックのセットアップ

  1. references/mocking/deep-mocked.md を読む - DeepMockedパターン
  2. references/mocking/jest-native.md を読む - ネイティブJestパターン
  3. references/mocking/factories.md を読む - テストデータファクトリ

リポジトリのテスト

  1. MongoDB: references/repository/mongodb.md
  2. PostgreSQL: references/repository/postgres.md

Kafkaのテスト (NestJSマイクロサービス)

  • references/kafka/kafka.md を読む - ClientKafkaモック、@MessagePattern/@EventPatternハンドラー、emit/sendテスト

Redisのテスト

  • references/redis/redis.md を読む - キャッシュ操作、ヘルスチェック、グレースフルデグラデーション

  • 包括的なパターンについては references/common/examples.md を読んでください。

テストパフォーマンスの最適化

  1. references/common/performance-optimization.md を読む - ワーカー設定、キャッシング、CI最適化
  2. references/common/detect-open-handles.md を読む - クリーンな終了を妨げるオープンハンドルを修正

オープンハンドルのデバッグ

  • references/common/detect-open-handles.md を読む - 検出コマンド、一般的なハンドルタイプ、クリーンアップパターン

コア原則

0. コンテキスト効率 (一時ファイル出力)

常に単体テストの出力をコンソールではなく一時ファイルにリダイレクトしてください。テスト出力は冗長になり、エージェントのコンテキストを肥大化させる可能性があります。

重要: 複数のエージェントが実行される際の競合を防ぐため、ファイル名に一意のセッションIDを使用してください。


# セッションの初期化 (テストセッションの開始時に一度だけ)
export UT_SESSION=$(date +%s)-$$

# 標準パターン - 出力を一時ファイルにリダイレクト (コンソール出力なし)
npm test > /tmp/ut-${UT_SESSION}-output.log 2>&1

# 概要のみを読み込む (最後の50行)
tail -50 /tmp/ut-${UT_SESSION}-outp

(原文がここで切り詰められています)
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Unit Testing Skill

Unit testing validates individual functions, methods, and classes in isolation by mocking all external dependencies.


Workflows

For guided, step-by-step execution of unit testing tasks, use the appropriate workflow:

Workflow Purpose When to Use
Setup Initialize test infrastructure New project or missing test setup
Writing Write new unit tests Creating tests for components
Reviewing Review existing tests Code review, quality audit
Running Execute tests Running tests, analyzing results
Debugging Fix failing tests Tests failing, need diagnosis
Optimizing Improve test performance Slow tests, maintainability

Workflow Selection Guide

IMPORTANT: Before starting any testing task, identify the user's intent and load the appropriate workflow.

Detect User Intent → Select Workflow

User Says / Wants Workflow to Load File
"Set up tests", "configure Jest", "add testing to project", "install test dependencies" Setup workflows/setup/workflow.md
"Write tests", "add tests", "create tests", "test this service/controller" Writing workflows/writing/workflow.md
"Review tests", "check test quality", "audit tests", "are these tests good?" Reviewing workflows/reviewing/workflow.md
"Run tests", "execute tests", "check if tests pass", "show test results" Running workflows/running/workflow.md
"Fix tests", "debug tests", "tests are failing", "why is this test broken?" Debugging workflows/debugging/workflow.md
"Speed up tests", "optimize tests", "tests are slow", "fix open handles" Optimizing workflows/optimizing/workflow.md

Workflow Execution Protocol

  1. ALWAYS load the workflow file first - Read the full workflow before taking action
  2. Follow each step in order - Complete checkpoints before proceeding
  3. Load knowledge files as directed - Each workflow specifies which references/ files to read
  4. Verify compliance after completion - Re-read relevant reference files to ensure quality

Knowledge Base Structure

references/
├── common/              # Core testing fundamentals
│   ├── knowledge.md     # Testing philosophy and test pyramid
│   ├── rules.md         # Mandatory testing rules (AAA, naming, coverage)
│   ├── assertions.md    # Assertion patterns and matchers
│   ├── examples.md      # Comprehensive examples by category
│   ├── detect-open-handles.md   # Open handle detection and cleanup
│   └── performance-optimization.md  # Jest runtime optimization
│
├── nestjs/              # NestJS component testing
│   ├── services.md      # Service/usecase testing patterns
│   ├── controllers.md   # Controller testing patterns
│   ├── guards.md        # Guard testing patterns
│   ├── interceptors.md  # Interceptor testing patterns
│   └── pipes-filters.md # Pipe and filter testing
│
├── mocking/             # Mock patterns and strategies
│   ├── deep-mocked.md   # @golevelup/ts-jest patterns
│   ├── jest-native.md   # Jest.fn, spyOn, mock patterns
│   └── factories.md     # Test data factory patterns
│
├── repository/          # Repository testing
│   ├── mongodb.md       # mongodb-memory-server patterns
│   └── postgres.md      # pg-mem patterns
│
├── kafka/               # NestJS Kafka microservices testing
│   └── kafka.md         # ClientKafka, @MessagePattern, @EventPattern handlers
│
└── redis/               # Redis cache testing
    └── redis.md         # Cache operations, health checks, graceful degradation

Quick Reference by Task

Write Unit Tests

  1. MANDATORY: Read references/common/rules.md - AAA pattern, naming, coverage
  2. Read references/common/assertions.md - Assertion best practices
  3. Read component-specific files:
    • Services: references/nestjs/services.md
    • Controllers: references/nestjs/controllers.md
    • Guards: references/nestjs/guards.md
    • Interceptors: references/nestjs/interceptors.md
    • Pipes/Filters: references/nestjs/pipes-filters.md

Setup Mocking

  1. Read references/mocking/deep-mocked.md - DeepMocked patterns
  2. Read references/mocking/jest-native.md - Native Jest patterns
  3. Read references/mocking/factories.md - Test data factories

Test Repositories

  1. MongoDB: references/repository/mongodb.md
  2. PostgreSQL: references/repository/postgres.md

Test Kafka (NestJS Microservices)

  • Read references/kafka/kafka.md - ClientKafka mocking, @MessagePattern/@EventPattern handlers, emit/send testing

Test Redis

  • Read references/redis/redis.md - Cache operations, health checks, graceful degradation

Examples

  • Read references/common/examples.md for comprehensive patterns

Optimize Test Performance

  1. Read references/common/performance-optimization.md - Worker config, caching, CI optimization
  2. Read references/common/detect-open-handles.md - Fix open handles preventing clean exit

Debug Open Handles

  • Read references/common/detect-open-handles.md - Detection commands, common handle types, cleanup patterns

Core Principles

0. Context Efficiency (Temp File Output)

ALWAYS redirect unit test output to temp files, NOT console. Test output can be verbose and bloats agent context.

IMPORTANT: Use unique session ID in filenames to prevent conflicts when multiple agents run.

# Initialize session (once at start of testing session)
export UT_SESSION=$(date +%s)-$$

# Standard pattern - redirect output to temp file (NO console output)
npm test > /tmp/ut-${UT_SESSION}-output.log 2>&1

# Read summary only (last 50 lines)
tail -50 /tmp/ut-${UT_SESSION}-output.log

# Get failure details
grep -B 2 -A 15 "FAIL\|✕" /tmp/ut-${UT_SESSION}-output.log

# Cleanup when done
rm -f /tmp/ut-${UT_SESSION}-*.log /tmp/ut-${UT_SESSION}-*.md

Temp Files (with ${UT_SESSION} unique per agent):

  • /tmp/ut-${UT_SESSION}-output.log - Full test output
  • /tmp/ut-${UT_SESSION}-failures.md - Tracking file for one-by-one fixing
  • /tmp/ut-${UT_SESSION}-debug.log - Debug runs
  • /tmp/ut-${UT_SESSION}-verify.log - Verification runs
  • /tmp/ut-${UT_SESSION}-coverage.log - Coverage output

1. AAA Pattern (Mandatory)

ALL unit tests MUST follow Arrange-Act-Assert:

it('should return user when found', async () => {
  // Arrange
  const userId = 'user-123';
  mockRepository.findById.mockResolvedValue({
    id: userId,
    email: 'test@example.com',
    name: 'Test User',
  });

  // Act
  const result = await target.getUser(userId);

  // Assert
  expect(result).toEqual({
    id: userId,
    email: 'test@example.com',
    name: 'Test User',
  });
  expect(mockRepository.findById).toHaveBeenCalledWith(userId);
});

2. Use target for SUT

Always name the system under test as target:

let target: UserService;
let mockRepository: DeepMocked<UserRepository>;

3. DeepMocked Pattern

Use @golevelup/ts-jest for type-safe mocks:

import { createMock, DeepMocked } from '@golevelup/ts-jest';

let mockService: DeepMocked<UserService>;

beforeEach(() => {
  mockService = createMock<UserService>();
});

4. Specific Assertions

Assert exact values, not just existence:

// WRONG
expect(result).toBeDefined();
expect(result.id).toBeDefined();

// CORRECT
expect(result).toEqual({
  id: 'user-123',
  email: 'test@example.com',
  name: 'Test User',
});

5. Mock All Dependencies

Mock external services, never real databases for unit tests:

// Unit Test: Mock repository
{ provide: UserRepository, useValue: mockRepository }

// Repository Test: Use in-memory database
const mongoServer = await createMongoMemoryServer();

Standard Test Template

import { Test, TestingModule } from '@nestjs/testing';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { MockLoggerService } from 'src/shared/logger/services/mock-logger.service';

describe('UserService', () => {
  let target: UserService;
  let mockRepository: DeepMocked<UserRepository>;

  beforeEach(async () => {
    // Arrange: Create mocks
    mockRepository = createMock<UserRepository>();

    const module: TestingModule = await Test.createTestingModule({
      providers: [
        UserService,
        { provide: UserRepository, useValue: mockRepository },
      ],
    })
      .setLogger(new MockLoggerService())
      .compile();

    target = module.get<UserService>(UserService);
  });

  afterEach(() => {
    jest.clearAllMocks();
  });

  describe('getUser', () => {
    it('should return user when found', async () => {
      // Arrange
      mockRepository.findById.mockResolvedValue({
        id: 'user-123',
        email: 'test@example.com',
      });

      // Act
      const result = await target.getUser('user-123');

      // Assert
      expect(result).toEqual({ id: 'user-123', email: 'test@example.com' });
    });

    it('should throw NotFoundException when user not found', async () => {
      // Arrange
      mockRepository.findById.mockResolvedValue(null);

      // Act & Assert
      await expect(target.getUser('invalid')).rejects.toThrow(NotFoundException);
    });
  });
});

Test Coverage Requirements

Category Priority Description
Happy path MANDATORY Valid inputs producing expected outputs
Edge cases MANDATORY Empty arrays, null values, boundaries
Error cases MANDATORY Not found, validation failures
Exception behavior MANDATORY Correct type, error code, message
Business rules MANDATORY Domain logic, calculations
Input validation MANDATORY Invalid inputs, type mismatches

Coverage Target: 80%+ for new code


Failure Resolution Protocol

CRITICAL: Fix ONE test at a time. NEVER run full suite repeatedly while fixing.

When unit tests fail:

  1. Initialize session (once at start):
    export UT_SESSION=$(date +%s)-$$
  2. Create tracking file: /tmp/ut-${UT_SESSION}-failures.md with all failing tests
  3. Select ONE failing test - work on only this test
  4. Run ONLY that test (never full suite):
    npm test -- -t "test name" > /tmp/ut-${UT_SESSION}-debug.log 2>&1
    tail -50 /tmp/ut-${UT_SESSION}-debug.log
  5. Fix the issue - analyze error, make targeted fix
  6. Verify fix - run same test 3-5 times:
    for i in {1..5}; do npm test -- -t "test name" > /tmp/ut-${UT_SESSION}-run$i.log 2>&1 && echo "Run $i: PASS" || echo "Run $i: FAIL"; done
  7. Mark as FIXED in tracking file
  8. Move to next failing test - repeat steps 3-7
  9. Run full suite ONLY ONCE after ALL individual tests pass
  10. Cleanup: rm -f /tmp/ut-${UT_SESSION}-*.log /tmp/ut-${UT_SESSION}-*.md

WHY: Running full suite wastes time and context. Each failing test pollutes output, making debugging harder.


Naming Conventions

Test Files

  • Pattern: *.spec.ts
  • Location: Co-located with source file

Test Structure

describe('ClassName', () => {
  describe('methodName', () => {
    it('should [expected behavior] when [condition]', () => {});
  });
});

Variable Names

Variable Convention
SUT target
Mocks mock prefix (mockRepository, mockService)
Mock Type DeepMocked<T>

What NOT to Unit Test

Do NOT create unit tests for:

  • Interfaces - Type definitions only, no runtime behavior
  • Enums - Static value mappings, no logic to test
  • Constants - Static values, no behavior
  • Type aliases - Type definitions only
  • Plain DTOs - Data structures without logic

Only test files containing executable logic (classes with methods, functions with behavior).


Anti-Patterns to Avoid

Don't Why Do Instead
Assert only existence Doesn't catch wrong values Assert specific values
Conditional assertions Non-deterministic Separate test cases
Test private methods Couples to implementation Test via public interface
Share state between tests Causes flaky tests Fresh setup in beforeEach
Mock repositories in services Tests implementation Mock interfaces
Skip mock verification Doesn't validate behavior Verify mock calls
Test interfaces/enums/constants No behavior to test Skip these files

Checklist

Setup:

  • [ ] Use target for system under test
  • [ ] Use mock prefix for all mocks
  • [ ] Use DeepMocked<T> type
  • [ ] Include .setLogger(new MockLoggerService())
  • [ ] Follow AAA pattern with comments
  • [ ] Reset mocks in afterEach

Coverage:

  • [ ] Happy path tests for all public methods
  • [ ] Edge case tests (empty, null, boundaries)
  • [ ] Error case tests (not found, validation failures)
  • [ ] Exception type and error code verification
  • [ ] Mock call verification (parameters + count)

Quality:

  • [ ] 80%+ coverage on new code
  • [ ] No assertions on log calls
  • [ ] No test interdependence
  • [ ] Tests fail when any field differs

同梱ファイル

※ ZIPに含まれるファイル一覧。`SKILL.md` 本体に加え、参考資料・サンプル・スクリプトが入っている場合があります。