From 16ea233eabb44d59ad1771ba2a4fb1d1e9118a05 Mon Sep 17 00:00:00 2001 From: jedarden Date: Fri, 22 May 2026 17:43:42 -0400 Subject: [PATCH] test(bf-5klc): fix vitest mock hoisting errors Fix mock hoisting issues in CrossReferencePanel and WorkerAnalyticsPanel test files by: - Defining mock classes entirely inline within vi.mock() factory functions - Removing references to external consts that were causing "Cannot access before initialization" errors - Simplifying getMockFunctions() helper to use the already-imported mocked constructor The vitest hoisting mechanism moves vi.mock() calls to the top of the file before regular variable declarations, so any consts referenced in the mock factory must also be defined inside the factory or via vi.hoisted(). The simplest fix is to define everything inline. Co-Authored-By: Claude Opus 4.7 --- .../components/CrossReferencePanel.test.ts | 52 +++++++++---------- .../components/WorkerAnalyticsPanel.test.ts | 3 +- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/tui/components/CrossReferencePanel.test.ts b/src/tui/components/CrossReferencePanel.test.ts index bb8641a..9e45b78 100644 --- a/src/tui/components/CrossReferencePanel.test.ts +++ b/src/tui/components/CrossReferencePanel.test.ts @@ -64,26 +64,21 @@ vi.mock('../utils/colors.js', () => ({ })); // Mock crossReferenceManager module - define everything inline to avoid hoisting issues -let mockManagerInstance: any = null; - -const mockGetEntity = vi.fn(function() { return null; }); -const mockGetLinksForEntity = vi.fn(function() { return []; }); -const mockGetStats = vi.fn(function() { return ({ - totalLinks: 0, - totalEntities: 0, - byRelationship: {}, - byEntityType: {}, - mostLinked: [], - recentLinks: [], -});}); -const mockFindPath = vi.fn(function() { return null; }); - vi.mock('../../crossReferenceManager.js', () => { + let mockManagerInstance: any = null; + class MockCrossReferenceManager { - getEntity = mockGetEntity; - getLinksForEntity = mockGetLinksForEntity; - getStats = mockGetStats; - findPath = mockFindPath; + getEntity = vi.fn(function() { return null; }); + getLinksForEntity = vi.fn(function() { return []; }); + getStats = vi.fn(function() { return ({ + totalLinks: 0, + totalEntities: 0, + byRelationship: {}, + byEntityType: {}, + mostLinked: [], + recentLinks: [], + });}); + findPath = vi.fn(function() { return null; }); } const MockConstructor = vi.fn(function() { @@ -95,13 +90,10 @@ vi.mock('../../crossReferenceManager.js', () => { return { CrossReferenceManager: MockConstructor, - MockCrossReferenceManager, + default: MockConstructor, }; }); -// Export the mock functions for test access -export { mockGetEntity, mockGetLinksForEntity, mockGetStats, mockFindPath }; - // Import after mocking import { CrossReferencePanel, createCrossReferencePanel } from './CrossReferencePanel.js'; import { CrossReferenceManager } from '../../crossReferenceManager.js'; @@ -111,12 +103,16 @@ import type { CrossReferenceEntity, CrossReferenceEntityType } from '../../types const MockCrossReferenceManagerConstructor = CrossReferenceManager as unknown as Mock; // Helper to get the mock functions from the singleton instance -const getMockFunctions = () => ({ - getEntity: mockGetEntity, - getLinksForEntity: mockGetLinksForEntity, - getStats: mockGetStats, - findPath: mockFindPath, -}); +const getMockFunctions = () => { + // The mocked module is already imported above, just use it + const instance = new MockCrossReferenceManagerConstructor(); + return { + getEntity: instance.getEntity, + getLinksForEntity: instance.getLinksForEntity, + getStats: instance.getStats, + findPath: instance.findPath, + }; +}; // Helper to create mock screen function createMockScreen() { diff --git a/src/tui/components/WorkerAnalyticsPanel.test.ts b/src/tui/components/WorkerAnalyticsPanel.test.ts index 952a1bc..3b47040 100644 --- a/src/tui/components/WorkerAnalyticsPanel.test.ts +++ b/src/tui/components/WorkerAnalyticsPanel.test.ts @@ -71,7 +71,7 @@ vi.mock('../utils/colors.js', () => ({ }, })); -// Mock workerAnalytics module - define everything inline inside the factory to avoid hoisting issues +// Mock workerAnalytics module - define everything inline to avoid hoisting issues vi.mock('../../workerAnalytics.js', () => { class MockWorkerAnalytics { compareWorkers = vi.fn(() => ({ @@ -87,6 +87,7 @@ vi.mock('../../workerAnalytics.js', () => { return { WorkerAnalytics: MockWorkerAnalytics, + default: MockWorkerAnalytics, }; });