Skip to content

Commit e34c0cc

Browse files
committed
fix(test): correct mock setup for scan tests
Replace setupStandardOutputMocks() helper with explicit top-level vi.mock() declarations to ensure proper Vitest mock hoisting. Update all tests to use vi.importMock() for mocked dependencies and dynamic imports for functions under test. Fixes 2 test files with 19 tests: - fetch-diff-scan.test.mts (8 tests) - output-create-new-scan.test.mts (11 tests)
1 parent d968484 commit e34c0cc

File tree

2 files changed

+62
-39
lines changed

2 files changed

+62
-39
lines changed

packages/cli/src/commands/scan/fetch-diff-scan.test.mts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { describe, expect, it, vi } from 'vitest'
22

3-
import { setupStandardOutputMocks } from '../../../test/helpers/mock-setup.mts'
4-
53
// Mock the dependencies.
6-
setupStandardOutputMocks()
4+
vi.mock('@socketsecurity/lib/logger', () => ({
5+
logger: {
6+
fail: vi.fn(),
7+
info: vi.fn(),
8+
log: vi.fn(),
9+
},
10+
}))
711

812
vi.mock('../../utils/socket/api.mts', () => ({
913
queryApiSafeJson: vi.fn(),
@@ -12,8 +16,8 @@ vi.mock('../../utils/socket/api.mts', () => ({
1216
describe('fetchDiffScan', () => {
1317
it('fetches diff scan successfully', async () => {
1418
const { fetchDiffScan } = await import('./fetch-diff-scan.mts')
15-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
16-
const { logger } = await import('@socketsecurity/lib/logger')
19+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
20+
const { logger } = await vi.importMock('@socketsecurity/lib/logger')
1721
const mockQueryApi = vi.mocked(queryApiSafeJson)
1822
const mockLogger = vi.mocked(logger.info)
1923

@@ -53,7 +57,7 @@ describe('fetchDiffScan', () => {
5357

5458
it('handles API call failure', async () => {
5559
const { fetchDiffScan } = await import('./fetch-diff-scan.mts')
56-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
60+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
5761
const mockQueryApi = vi.mocked(queryApiSafeJson)
5862

5963
const error = {
@@ -75,7 +79,7 @@ describe('fetchDiffScan', () => {
7579

7680
it('properly URL encodes scan IDs', async () => {
7781
const { fetchDiffScan } = await import('./fetch-diff-scan.mts')
78-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
82+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
7983
const mockQueryApi = vi.mocked(queryApiSafeJson)
8084

8185
mockQueryApi.mockResolvedValue({
@@ -100,7 +104,7 @@ describe('fetchDiffScan', () => {
100104

101105
it('handles different org slugs', async () => {
102106
const { fetchDiffScan } = await import('./fetch-diff-scan.mts')
103-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
107+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
104108
const mockQueryApi = vi.mocked(queryApiSafeJson)
105109

106110
mockQueryApi.mockResolvedValue({
@@ -132,7 +136,7 @@ describe('fetchDiffScan', () => {
132136

133137
it('handles empty diff results', async () => {
134138
const { fetchDiffScan } = await import('./fetch-diff-scan.mts')
135-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
139+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
136140
const mockQueryApi = vi.mocked(queryApiSafeJson)
137141

138142
const emptyDiffData = {
@@ -162,8 +166,8 @@ describe('fetchDiffScan', () => {
162166

163167
it('handles same scan IDs gracefully', async () => {
164168
const { fetchDiffScan } = await import('./fetch-diff-scan.mts')
165-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
166-
const { logger } = await import('@socketsecurity/lib/logger')
169+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
170+
const { logger } = await vi.importMock('@socketsecurity/lib/logger')
167171
const mockQueryApi = vi.mocked(queryApiSafeJson)
168172
const mockLogger = vi.mocked(logger.info)
169173

@@ -193,7 +197,7 @@ describe('fetchDiffScan', () => {
193197

194198
it('handles server timeout gracefully', async () => {
195199
const { fetchDiffScan } = await import('./fetch-diff-scan.mts')
196-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
200+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
197201
const mockQueryApi = vi.mocked(queryApiSafeJson)
198202

199203
const timeoutError = {
@@ -215,7 +219,7 @@ describe('fetchDiffScan', () => {
215219

216220
it('uses null prototype internally', async () => {
217221
const { fetchDiffScan } = await import('./fetch-diff-scan.mts')
218-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
222+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
219223
const mockQueryApi = vi.mocked(queryApiSafeJson)
220224

221225
mockQueryApi.mockResolvedValue({

packages/cli/src/commands/scan/output-create-new-scan.test.mts

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import { beforeEach, describe, expect, it, vi } from 'vitest'
22

3-
import { outputCreateNewScan } from './output-create-new-scan.mts'
4-
import { setupStandardOutputMocks } from '../../../test/helpers/index.mts'
5-
63
import type { CResult } from '../../types.mts'
74
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
85

96
// Mock the dependencies.
10-
setupStandardOutputMocks()
7+
vi.mock('@socketsecurity/lib/logger', () => ({
8+
logger: {
9+
fail: vi.fn(),
10+
log: vi.fn(),
11+
success: vi.fn(),
12+
},
13+
}))
14+
15+
vi.mock('../../utils/output/result-json.mjs', () => ({
16+
serializeResultJson: vi.fn(result => JSON.stringify(result)),
17+
}))
18+
19+
vi.mock('../../utils/error/fail-msg-with-badge.mts', () => ({
20+
failMsgWithBadge: vi.fn((msg, cause) => `${msg}: ${cause}`),
21+
}))
1122

1223
vi.mock('open', () => ({
1324
default: vi.fn(),
@@ -37,9 +48,10 @@ describe('outputCreateNewScan', () => {
3748
})
3849

3950
it('outputs JSON format for successful result', async () => {
40-
const { logger } = await import('@socketsecurity/lib/logger')
41-
const { serializeResultJson } = await import(
42-
'../../utils/serialize/result-json.mts'
51+
const { outputCreateNewScan } = await import('./output-create-new-scan.mts')
52+
const { logger } = await vi.importMock('@socketsecurity/lib/logger')
53+
const { serializeResultJson } = await vi.importMock(
54+
'../../utils/output/result-json.mjs'
4355
)
4456
const mockLog = vi.mocked(logger.log)
4557
const mockSerialize = vi.mocked(serializeResultJson)
@@ -61,7 +73,8 @@ describe('outputCreateNewScan', () => {
6173
})
6274

6375
it('outputs error in JSON format', async () => {
64-
const { logger } = await import('@socketsecurity/lib/logger')
76+
const { outputCreateNewScan } = await import('./output-create-new-scan.mts')
77+
const { logger } = await vi.importMock('@socketsecurity/lib/logger')
6578
const mockLog = vi.mocked(logger.log)
6679

6780
const result: CResult<SocketSdkSuccessResult<'CreateOrgFullScan'>['data']> =
@@ -79,8 +92,9 @@ describe('outputCreateNewScan', () => {
7992
})
8093

8194
it('outputs success message with report URL in text format', async () => {
82-
const { logger } = await import('@socketsecurity/lib/logger')
83-
const terminalLink = await import('terminal-link')
95+
const { outputCreateNewScan } = await import('./output-create-new-scan.mts')
96+
const { logger } = await vi.importMock('@socketsecurity/lib/logger')
97+
const terminalLink = await vi.importMock('terminal-link')
8498
const mockLog = vi.mocked(logger.log)
8599
const mockSuccess = vi.mocked(logger.success)
86100
const mockTerminalLink = vi.mocked(terminalLink.default)
@@ -107,7 +121,8 @@ describe('outputCreateNewScan', () => {
107121
})
108122

109123
it('outputs markdown format with scan ID', async () => {
110-
const { logger } = await import('@socketsecurity/lib/logger')
124+
const { outputCreateNewScan } = await import('./output-create-new-scan.mts')
125+
const { logger } = await vi.importMock('@socketsecurity/lib/logger')
111126
const mockLog = vi.mocked(logger.log)
112127

113128
const result: CResult<SocketSdkSuccessResult<'CreateOrgFullScan'>['data']> =
@@ -130,7 +145,8 @@ describe('outputCreateNewScan', () => {
130145
})
131146

132147
it('handles missing scan ID properly', async () => {
133-
const { logger } = await import('@socketsecurity/lib/logger')
148+
const { outputCreateNewScan } = await import('./output-create-new-scan.mts')
149+
const { logger } = await vi.importMock('@socketsecurity/lib/logger')
134150
const mockFail = vi.mocked(logger.fail)
135151

136152
const result: CResult<SocketSdkSuccessResult<'CreateOrgFullScan'>['data']> =
@@ -151,8 +167,9 @@ describe('outputCreateNewScan', () => {
151167
})
152168

153169
it('outputs error in text format', async () => {
154-
const { logger } = await import('@socketsecurity/lib/logger')
155-
const { failMsgWithBadge } = await import(
170+
const { outputCreateNewScan } = await import('./output-create-new-scan.mts')
171+
const { logger } = await vi.importMock('@socketsecurity/lib/logger')
172+
const { failMsgWithBadge } = await vi.importMock(
156173
'../../utils/error/fail-msg-with-badge.mts'
157174
)
158175
const mockFail = vi.mocked(logger.fail)
@@ -177,8 +194,9 @@ describe('outputCreateNewScan', () => {
177194
})
178195

179196
it('opens browser when interactive and user confirms', async () => {
180-
const { confirm } = await import('@socketsecurity/lib/prompts')
181-
const open = await import('open')
197+
const { outputCreateNewScan } = await import('./output-create-new-scan.mts')
198+
const { confirm } = await vi.importMock('@socketsecurity/lib/prompts')
199+
const open = await vi.importMock('open')
182200
const mockConfirm = vi.mocked(confirm)
183201
const mockOpen = vi.mocked(open.default)
184202

@@ -198,21 +216,19 @@ describe('outputCreateNewScan', () => {
198216
outputKind: 'text',
199217
})
200218

201-
expect(mockConfirm).toHaveBeenCalledWith(
202-
{
203-
default: false,
204-
message: 'Would you like to open it in your browser?',
205-
},
206-
{ spinner: expect.any(Object) },
207-
)
219+
expect(mockConfirm).toHaveBeenCalledWith({
220+
default: false,
221+
message: 'Would you like to open it in your browser?',
222+
})
208223
expect(mockOpen).toHaveBeenCalledWith(
209224
'https://socket.dev/report/browser-test',
210225
)
211226
})
212227

213228
it('does not open browser when user declines', async () => {
214-
const { confirm } = await import('@socketsecurity/lib/prompts')
215-
const open = await import('open')
229+
const { outputCreateNewScan } = await import('./output-create-new-scan.mts')
230+
const { confirm } = await vi.importMock('@socketsecurity/lib/prompts')
231+
const open = await vi.importMock('open')
216232
const mockConfirm = vi.mocked(confirm)
217233
const mockOpen = vi.mocked(open.default)
218234

@@ -237,6 +253,7 @@ describe('outputCreateNewScan', () => {
237253
})
238254

239255
it('handles spinner lifecycle correctly', async () => {
256+
const { outputCreateNewScan } = await import('./output-create-new-scan.mts')
240257
mockSpinner.isSpinning = true
241258

242259
const result: CResult<SocketSdkSuccessResult<'CreateOrgFullScan'>['data']> =
@@ -258,7 +275,8 @@ describe('outputCreateNewScan', () => {
258275
})
259276

260277
it('handles missing report URL', async () => {
261-
const { logger } = await import('@socketsecurity/lib/logger')
278+
const { outputCreateNewScan } = await import('./output-create-new-scan.mts')
279+
const { logger } = await vi.importMock('@socketsecurity/lib/logger')
262280
const mockLog = vi.mocked(logger.log)
263281

264282
const result: CResult<SocketSdkSuccessResult<'CreateOrgFullScan'>['data']> =
@@ -276,6 +294,7 @@ describe('outputCreateNewScan', () => {
276294
})
277295

278296
it('sets default exit code when code is undefined', async () => {
297+
const { outputCreateNewScan } = await import('./output-create-new-scan.mts')
279298
const result: CResult<SocketSdkSuccessResult<'CreateOrgFullScan'>['data']> =
280299
{
281300
ok: false,

0 commit comments

Comments
 (0)