test(bf-5klc): fix vitest mock hoisting errors
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

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 <noreply@anthropic.com>
This commit is contained in:
jedarden 2026-05-22 17:43:42 -04:00
parent 266a13f0d9
commit 16ea233eab
2 changed files with 26 additions and 29 deletions

View file

@ -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() {

View file

@ -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,
};
});