# アーキテクチャ構成図

## システム全体アーキテクチャ

```mermaid
graph TB
    subgraph "Entry Layer"
        CLI[jest-cli]
        JEST[jest]
        CREATE[create-jest]
    end

    subgraph "Orchestration Layer"
        CORE["@jest/core"]
        SCHEDULER[TestScheduler]
        SEARCH[SearchSource]
        SEQ["@jest/test-sequencer"]
    end

    subgraph "Runner Layer"
        RUNNER[jest-runner]
        CIRCUS[jest-circus]
        JASMINE[jest-jasmine2]
    end

    subgraph "Runtime Layer"
        RUNTIME[jest-runtime]
        RESOLVE[jest-resolve]
        MOCK[jest-mock]
        FAKE_TIMERS["@jest/fake-timers"]
    end

    subgraph "Environment Layer"
        ENV["@jest/environment"]
        ENV_NODE[jest-environment-node]
        ENV_JSDOM[jest-environment-jsdom]
    end

    subgraph "Transform Layer"
        TRANSFORM["@jest/transform"]
        BABEL[babel-jest]
        HASTE[jest-haste-map]
    end

    subgraph "Assertion Layer"
        EXPECT[expect]
        SNAPSHOT[jest-snapshot]
        MATCHER[jest-matcher-utils]
    end

    subgraph "Reporting Layer"
        REPORTERS["@jest/reporters"]
        CONSOLE["@jest/console"]
        RESULT["@jest/test-result"]
    end

    subgraph "Utility Layer"
        UTIL[jest-util]
        TYPES["@jest/types"]
        WORKER[jest-worker]
        CONFIG[jest-config]
        VALIDATE[jest-validate]
    end

    %% Entry to Orchestration
    JEST --> CLI
    CLI --> CORE
    CREATE --> CONFIG

    %% Orchestration Layer
    CORE --> SCHEDULER
    CORE --> SEARCH
    SCHEDULER --> SEQ
    SCHEDULER --> RUNNER

    %% Runner to Runtime
    RUNNER --> RUNTIME
    RUNNER --> CIRCUS
    RUNNER --> JASMINE
    RUNNER --> WORKER

    %% Runtime dependencies
    RUNTIME --> RESOLVE
    RUNTIME --> MOCK
    RUNTIME --> FAKE_TIMERS
    RUNTIME --> TRANSFORM

    %% Environment
    RUNNER --> ENV
    ENV --> ENV_NODE
    ENV --> ENV_JSDOM

    %% Transform
    TRANSFORM --> BABEL
    TRANSFORM --> HASTE

    %% Assertion
    CIRCUS --> EXPECT
    CIRCUS --> SNAPSHOT
    EXPECT --> MATCHER

    %% Reporting
    SCHEDULER --> REPORTERS
    REPORTERS --> CONSOLE
    REPORTERS --> RESULT

    %% Utility (used by many)
    CORE --> CONFIG
    CONFIG --> VALIDATE
    RUNNER --> UTIL
    RUNTIME --> UTIL

    classDef entryLayer fill:#e1f5fe,stroke:#01579b
    classDef orchestrationLayer fill:#f3e5f5,stroke:#4a148c
    classDef runnerLayer fill:#e8f5e9,stroke:#1b5e20
    classDef runtimeLayer fill:#fff3e0,stroke:#e65100
    classDef envLayer fill:#fce4ec,stroke:#880e4f
    classDef transformLayer fill:#e0f2f1,stroke:#004d40
    classDef assertionLayer fill:#fff8e1,stroke:#ff6f00
    classDef reportingLayer fill:#f1f8e9,stroke:#33691e
    classDef utilityLayer fill:#eceff1,stroke:#263238

    class CLI,JEST,CREATE entryLayer
    class CORE,SCHEDULER,SEARCH,SEQ orchestrationLayer
    class RUNNER,CIRCUS,JASMINE runnerLayer
    class RUNTIME,RESOLVE,MOCK,FAKE_TIMERS runtimeLayer
    class ENV,ENV_NODE,ENV_JSDOM envLayer
    class TRANSFORM,BABEL,HASTE transformLayer
    class EXPECT,SNAPSHOT,MATCHER assertionLayer
    class REPORTERS,CONSOLE,RESULT reportingLayer
    class UTIL,TYPES,WORKER,CONFIG,VALIDATE utilityLayer
```

## テスト実行フロー

```mermaid
sequenceDiagram
    participant User
    participant CLI as jest-cli
    participant Core as @jest/core
    participant Scheduler as TestScheduler
    participant Worker as jest-worker
    participant Runner as jest-runner
    participant Runtime as jest-runtime
    participant Circus as jest-circus
    participant Reporter as @jest/reporters

    User->>CLI: jest コマンド実行
    CLI->>CLI: 引数解析 (yargs)
    CLI->>Core: runCLI(argv, projects)

    Core->>Core: 設定読み込み (jest-config)
    Core->>Core: SearchSource でテスト発見
    Core->>Scheduler: createTestScheduler()

    Scheduler->>Scheduler: Reporter設定
    Scheduler->>Scheduler: テスト実行判定 (並列/直列)

    alt 並列実行
        Scheduler->>Worker: ワーカープール作成
        Worker->>Runner: テスト分散
    else 直列実行
        Scheduler->>Runner: 直接実行
    end

    Runner->>Runtime: 環境構築
    Runtime->>Runtime: モジュール解決
    Runtime->>Runtime: モック設定

    Runner->>Circus: テスト実行
    Circus->>Circus: describe/test 実行
    Circus->>Circus: expect アサーション
    Circus-->>Runner: TestResult

    Runner-->>Scheduler: 結果通知 (Emittery)
    Scheduler->>Reporter: onTestFileResult
    Reporter->>User: 結果出力
```

## パッケージ依存関係図

```mermaid
graph LR
    subgraph "Core Packages"
        jest --> jest-cli
        jest-cli --> core["@jest/core"]
        core --> runner[jest-runner]
        core --> config[jest-config]
        core --> haste[jest-haste-map]
        core --> reporters["@jest/reporters"]
    end

    subgraph "Runner Packages"
        runner --> runtime[jest-runtime]
        runner --> worker[jest-worker]
        runner --> circus[jest-circus]
        runner --> jasmine[jest-jasmine2]
    end

    subgraph "Runtime Packages"
        runtime --> resolve[jest-resolve]
        runtime --> mock[jest-mock]
        runtime --> transform["@jest/transform"]
        runtime --> snapshot[jest-snapshot]
    end

    subgraph "Assertion Packages"
        circus --> expect[expect]
        circus --> snapshot
        expect --> matcher[jest-matcher-utils]
    end

    subgraph "Transform Packages"
        transform --> babel[babel-jest]
        transform --> haste
    end

    subgraph "Shared Packages"
        util[jest-util]
        types["@jest/types"]
    end

    runtime --> util
    runner --> util
    core --> util
    config --> util

    circus --> types
    runner --> types
    core --> types
```

## ワーカープロセスアーキテクチャ

```mermaid
graph TB
    subgraph "Main Process"
        Main[TestScheduler]
        Pool[Worker Pool<br/>jest-worker]
    end

    subgraph "Worker Process 1"
        W1[Worker 1]
        R1[jest-runtime]
        C1[jest-circus]
    end

    subgraph "Worker Process 2"
        W2[Worker 2]
        R2[jest-runtime]
        C2[jest-circus]
    end

    subgraph "Worker Process N"
        WN[Worker N]
        RN[jest-runtime]
        CN[jest-circus]
    end

    Main --> Pool
    Pool --> W1
    Pool --> W2
    Pool --> WN

    W1 --> R1 --> C1
    W2 --> R2 --> C2
    WN --> RN --> CN

    C1 -.->|Emittery Event| Main
    C2 -.->|Emittery Event| Main
    CN -.->|Emittery Event| Main

    style Main fill:#e1f5fe
    style Pool fill:#e1f5fe
    style W1 fill:#e8f5e9
    style W2 fill:#e8f5e9
    style WN fill:#e8f5e9
```

## モジュール解決フロー

```mermaid
flowchart TB
    subgraph "Import/Require"
        REQ[require/import 呼び出し]
    end

    subgraph "jest-runtime"
        RUNTIME[Runtime._execModule]
        MOCK_CHECK{モック?}
        ESM_CHECK{ESM?}
    end

    subgraph "jest-resolve"
        RESOLVER[Resolver]
        NODE[Node.js Resolution]
        HASTE[Haste Map]
        MAPPER[Module Name Mapper]
    end

    subgraph "jest-mock"
        MOCK_FACTORY[Mock Factory]
        AUTO_MOCK[Auto Mock]
        MANUAL_MOCK[Manual Mock]
    end

    subgraph "@jest/transform"
        TRANSFORM[Script Transformer]
        CACHE[Transform Cache]
        BABEL[babel-jest]
    end

    REQ --> RUNTIME
    RUNTIME --> MOCK_CHECK

    MOCK_CHECK -->|Yes| MOCK_FACTORY
    MOCK_FACTORY --> AUTO_MOCK
    MOCK_FACTORY --> MANUAL_MOCK

    MOCK_CHECK -->|No| RESOLVER
    RESOLVER --> MAPPER
    MAPPER --> HASTE
    HASTE --> NODE

    NODE --> ESM_CHECK
    ESM_CHECK -->|CJS| TRANSFORM
    ESM_CHECK -->|ESM| TRANSFORM

    TRANSFORM --> CACHE
    CACHE -->|miss| BABEL
    BABEL --> CACHE
```

## テストライフサイクル (jest-circus)

```mermaid
stateDiagram-v2
    [*] --> describe_start: run_start

    describe_start --> beforeAll: run_describe_start
    beforeAll --> test_start: beforeAll hooks完了

    test_start --> beforeEach: test_start
    beforeEach --> test_fn: beforeEach hooks完了
    test_fn --> afterEach: テスト関数実行
    afterEach --> test_done: afterEach hooks完了

    test_done --> test_start: 次のテスト
    test_done --> afterAll: 全テスト完了

    afterAll --> describe_done: afterAll hooks完了
    describe_done --> describe_start: ネストした describe
    describe_done --> [*]: run_finish

    note right of test_fn
        expect() アサーション
        snapshot マッチング
    end note
```

## レポーター パイプライン

```mermaid
flowchart LR
    subgraph "Test Events"
        START[onRunStart]
        FILE_START[onTestFileStart]
        RESULT[onTestFileResult]
        COMPLETE[onRunComplete]
    end

    subgraph "ReporterDispatcher"
        DISPATCH[dispatch]
    end

    subgraph "Reporters"
        DEFAULT[DefaultReporter]
        VERBOSE[VerboseReporter]
        SUMMARY[SummaryReporter]
        COVERAGE[CoverageReporter]
        GITHUB[GitHubActionsReporter]
        NOTIFY[NotifyReporter]
        CUSTOM[Custom Reporter]
    end

    START --> DISPATCH
    FILE_START --> DISPATCH
    RESULT --> DISPATCH
    COMPLETE --> DISPATCH

    DISPATCH --> DEFAULT
    DISPATCH --> VERBOSE
    DISPATCH --> SUMMARY
    DISPATCH --> COVERAGE
    DISPATCH --> GITHUB
    DISPATCH --> NOTIFY
    DISPATCH --> CUSTOM

    DEFAULT -->|stdout| OUTPUT[Console Output]
    VERBOSE -->|stdout| OUTPUT
    SUMMARY -->|stdout| OUTPUT
    COVERAGE -->|file| COV_FILE[coverage/]
    GITHUB -->|stdout| GH_OUT[GitHub Actions Annotations]
    NOTIFY -->|system| NOTIF[Desktop Notification]
```

## 外部システム連携

```mermaid
graph TB
    subgraph "Jest Core"
        JEST[Jest]
    end

    subgraph "Build Tools"
        BABEL[Babel]
        TS[TypeScript]
        WEBPACK[Webpack]
    end

    subgraph "Coverage"
        ISTANBUL[Istanbul]
        V8[V8 Coverage]
    end

    subgraph "File System"
        WATCHMAN[Watchman]
        FSEVENTS[FSEvents]
        GIT[Git]
        HG[Mercurial]
    end

    subgraph "Environment"
        NODEJS[Node.js VM]
        JSDOM[jsdom]
    end

    subgraph "Notification"
        NOTIFIER[node-notifier]
    end

    JEST --> BABEL
    JEST --> TS
    JEST -.-> WEBPACK

    JEST --> ISTANBUL
    JEST --> V8

    JEST --> WATCHMAN
    JEST --> FSEVENTS
    JEST --> GIT
    JEST --> HG

    JEST --> NODEJS
    JEST --> JSDOM

    JEST -.-> NOTIFIER

    style JEST fill:#e1f5fe,stroke:#01579b
    style BABEL fill:#f9fbe7,stroke:#827717
    style TS fill:#e3f2fd,stroke:#1565c0
    style ISTANBUL fill:#fff3e0,stroke:#e65100
    style WATCHMAN fill:#fce4ec,stroke:#880e4f
```
