Skip to content

Commit 028d12e

Browse files
committed
refactor(test): remove all broken mock setup helpers
Remove all mock setup helper functions from test/helpers/mock-setup.mts because they don't work with Vitest. Vitest requires vi.mock() to be called at top level for proper hoisting - when called from within a function, mocks aren't initialized correctly, resulting in "No export defined on mock" errors. Replace with comprehensive documentation explaining: - Why these helpers don't work - The correct pattern for mocking in Vitest - Example code showing proper usage Breaking changes: - setupStandardOutputMocks() - removed - setupStandardSdkMocks() - removed - setupOutputWithTableMocks() - removed - setupDebugMocks() - removed - setupHandleFunctionMocks() - removed Affected tests that need fixing: - src/commands/package/fetch-purl-deep-score.test.mts (7 tests) - src/commands/scan/fetch-report-data.test.mts (8 tests)
1 parent fa4a138 commit 028d12e

File tree

1 file changed

+51
-74
lines changed

1 file changed

+51
-74
lines changed
Lines changed: 51 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,54 @@
1-
/** @fileoverview Standard mock setup utilities for Socket CLI tests. Provides reusable mock configurations for SDK, logger, output utilities, and debug functions. */
2-
3-
import { vi } from 'vitest'
4-
51
/**
6-
* Setup standard SDK and API mocks
7-
* Use this for tests that need to mock SDK setup and API calls
2+
* @fileoverview Mock setup utilities for Socket CLI tests.
3+
*
4+
* IMPORTANT: Mock setup helper functions DO NOT WORK with Vitest.
5+
*
6+
* Vitest requires vi.mock() to be called at the top level of test files for proper hoisting.
7+
* When vi.mock() is called from within a function, the mock declarations are not hoisted
8+
* correctly, resulting in "No export defined on mock" errors.
9+
*
10+
* Instead of using helper functions, explicitly declare mocks at the top of each test file.
11+
*
12+
* @example Correct pattern for mocking
13+
* ```typescript
14+
* import { beforeEach, describe, expect, it, vi } from 'vitest'
15+
*
16+
* // Mock declarations MUST be at top level
17+
* vi.mock('@socketsecurity/lib/logger', () => ({
18+
* logger: {
19+
* fail: vi.fn(),
20+
* log: vi.fn(),
21+
* },
22+
* }))
23+
*
24+
* vi.mock('../../utils/socket/api.mjs', () => ({
25+
* queryApiSafeJson: vi.fn(),
26+
* }))
27+
*
28+
* describe('myTest', () => {
29+
* beforeEach(() => {
30+
* vi.clearAllMocks()
31+
* })
32+
*
33+
* it('test name', async () => {
34+
* // Use dynamic imports for function under test
35+
* const { functionUnderTest } = await import('./module-under-test.mts')
36+
*
37+
* // Use vi.importMock for mocked dependencies
38+
* const { logger } = await vi.importMock('@socketsecurity/lib/logger')
39+
* const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mjs')
40+
*
41+
* const mockLog = vi.mocked(logger.log)
42+
* const mockQueryApi = vi.mocked(queryApiSafeJson)
43+
*
44+
* // ... test code
45+
* })
46+
* })
47+
* ```
48+
*
49+
* @see https://vitest.dev/api/vi.html#vi-mock
850
*/
9-
export function setupStandardSdkMocks() {
10-
vi.mock('../../src/utils/socket/api.mts', () => ({
11-
handleApiCall: vi.fn(),
12-
}))
13-
14-
vi.mock('../../src/utils/socket/sdk.mts', () => ({
15-
setupSdk: vi.fn(),
16-
withSdk: vi.fn(),
17-
}))
18-
}
1951

20-
/**
21-
* Setup standard output utility mocks
22-
* Use this for output-*.test.mts files
23-
*/
24-
export function setupStandardOutputMocks() {
25-
vi.mock('@socketsecurity/lib/logger', () => ({
26-
logger: {
27-
fail: vi.fn(),
28-
log: vi.fn(),
29-
info: vi.fn(),
30-
warn: vi.fn(),
31-
error: vi.fn(),
32-
success: vi.fn(),
33-
},
34-
}))
35-
36-
vi.mock('../../src/utils/error/fail-msg-with-badge.mts', () => ({
37-
failMsgWithBadge: vi.fn((msg, cause) => `${msg}: ${cause}`),
38-
}))
39-
40-
vi.mock('../../src/utils/output/result-json.mts', () => ({
41-
serializeResultJson: vi.fn(result => JSON.stringify(result)),
42-
}))
43-
}
44-
45-
/**
46-
* Setup output mocks with additional table formatting
47-
* Use this for output files that render tables
48-
*/
49-
export function setupOutputWithTableMocks() {
50-
setupStandardOutputMocks()
51-
52-
vi.mock('../../src/utils/output/markdown.mts', () => ({
53-
mdTableOfPairs: vi.fn(pairs => `Table with ${pairs.length} rows`),
54-
}))
55-
}
56-
57-
/**
58-
* Setup debug utility mocks
59-
* Use this for handle-*.test.mts files
60-
*/
61-
export function setupDebugMocks() {
62-
vi.mock('../../src/utils/debug.mts', () => ({
63-
debugDir: vi.fn(),
64-
debugFn: vi.fn(),
65-
debugLog: vi.fn(),
66-
isDebug: vi.fn(() => false),
67-
}))
68-
}
69-
70-
/**
71-
* Setup combined mocks for handle functions
72-
* Use this for handle-*.test.mts files that orchestrate fetch + output
73-
*/
74-
export function setupHandleFunctionMocks() {
75-
setupStandardSdkMocks()
76-
setupDebugMocks()
77-
}
52+
// This file intentionally left empty.
53+
// All previous mock setup helper functions have been removed because they don't work with Vitest.
54+
// See the file-level documentation above for the correct pattern.

0 commit comments

Comments
 (0)