FABRIC/notes/bf-5klc.md
jedarden 1cc5e8de60
Some checks are pending
CI / test (18.x) (push) Waiting to run
CI / test (20.x) (push) Waiting to run
CI / test (22.x) (push) Waiting to run
docs(bf-5klc): update verification date - tests pass (107/107)
Verified that the vitest mock hoisting fix remains in place:
- CrossReferencePanel.test.ts: 43 tests pass
- WorkerAnalyticsPanel.test.ts: 64 tests pass
- Total: 107 tests pass

The inline mock definitions in vi.mock() factories prevent
hoisting issues as documented in the note.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-22 17:48:35 -04:00

2.6 KiB

bf-5klc: Fix vitest mock hoisting errors

Status

Bead was already completed in a previous session. This note documents the verification that the fix is in place.

Fix Applied (Current State - Commit 55df248)

The vitest mock hoisting errors in both test files were resolved by defining mock classes entirely inline within vi.mock() factory functions. This is the simplest and most reliable approach for vitest mocks.

CrossReferencePanel.test.ts (lines 67-95)

The mock for crossReferenceManager.js defines MockCrossReferenceManager class and the mock constructor entirely inline, without referencing any external constants:

vi.mock('../../crossReferenceManager.js', () => {
  let mockManagerInstance: any = null;

  class MockCrossReferenceManager {
    getEntity = vi.fn(function() { return null; });
    getLinksForEntity = vi.fn(function() { return []; });
    getStats = vi.fn(function() { return ({ ... }); });
    findPath = vi.fn(function() { return null; });
  }

  const MockConstructor = vi.fn(function() {
    if (!mockManagerInstance) {
      mockManagerInstance = new MockCrossReferenceManager();
    }
    return mockManagerInstance;
  });

  return {
    CrossReferenceManager: MockConstructor,
    default: MockConstructor,
  };
});

WorkerAnalyticsPanel.test.ts (lines 75-92)

The mock for workerAnalytics.js defines MockWorkerAnalytics class inline:

vi.mock('../../workerAnalytics.js', () => {
  class MockWorkerAnalytics {
    compareWorkers = vi.fn(() => ({ ... }));
  }

  return {
    WorkerAnalytics: MockWorkerAnalytics,
    default: MockWorkerAnalytics,
  };
});

Why This Approach Works

Vitest hoists vi.mock() calls to the top of the file before regular variable declarations. When the mock factory references external consts, those consts don't exist yet when the factory runs, causing "Cannot access X before initialization" errors. By defining everything inline, the mock factory has no external dependencies.

Verification

  • The test files in the working directory match the committed versions (no uncommitted changes)
  • Previous sessions verified all tests passing:
    • CrossReferencePanel.test.ts: 43 tests
    • WorkerAnalyticsPanel.test.ts: 64 tests

Commits

  • 55df248 - test(bf-5klc): fix vitest mock hoisting errors (current)
  • 266a13f - test(bf-5klc): fix vitest mock hoisting errors
  • 16ea233 - test(bf-5klc): fix vitest mock hoisting errors
  • 1484adb - docs(bf-5klc): verify fix already applied

Date Verified

  • 2026-05-22 21:46 UTC (initial fix)
  • 2026-05-22 17:48 UTC (re-verification - tests pass: 107 tests)