Skip to content

Commit 6b8b6a4

Browse files
committed
fix(tests): use vi.importMock() consistently in fetch-update-repo tests
- Replace await import() with vi.importMock() throughout - Add beforeEach to clear mocks between tests - Replace helper functions with direct mock setups - All 8 tests now pass
1 parent f714cf7 commit 6b8b6a4

File tree

2 files changed

+503
-26
lines changed

2 files changed

+503
-26
lines changed

packages/cli/src/commands/repository/fetch-update-repo.test.mts

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

33
import { fetchUpdateRepo } from './fetch-update-repo.mts'
4-
import { createSuccessResult } from '../../../test/helpers/mocks.mts'
54
import {
6-
setupSdkMockError,
7-
setupSdkSetupFailure,
8-
} from '../../../test/helpers/sdk-test-helpers.mts'
5+
createErrorResult,
6+
createSuccessResult,
7+
} from '../../../test/helpers/mocks.mts'
98

109
// Mock the dependencies.
1110
vi.mock('../../utils/socket/api.mts', () => ({
@@ -17,9 +16,13 @@ vi.mock('../../utils/socket/sdk.mts', () => ({
1716
}))
1817

1918
describe('fetchUpdateRepo', () => {
19+
beforeEach(() => {
20+
vi.clearAllMocks()
21+
})
22+
2023
it('updates repository successfully', async () => {
21-
const { handleApiCall } = await import('../../utils/socket/api.mts')
22-
const { setupSdk } = await import('../../utils/socket/sdk.mts')
24+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
25+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
2326
const mockHandleApi = vi.mocked(handleApiCall)
2427
const mockSetupSdk = vi.mocked(setupSdk)
2528

@@ -74,10 +77,15 @@ describe('fetchUpdateRepo', () => {
7477
})
7578

7679
it('handles SDK setup failure', async () => {
77-
await setupSdkSetupFailure('Failed to setup SDK', {
78-
code: 1,
79-
cause: 'Missing API token',
80-
})
80+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
81+
const mockSetupSdk = vi.mocked(setupSdk)
82+
83+
mockSetupSdk.mockResolvedValue(
84+
createErrorResult('Failed to setup SDK', {
85+
code: 1,
86+
cause: 'Missing API token',
87+
}),
88+
)
8189

8290
const config = {
8391
defaultBranch: 'main',
@@ -94,10 +102,18 @@ describe('fetchUpdateRepo', () => {
94102
})
95103

96104
it('handles API call failure', async () => {
97-
await setupSdkMockError(
98-
'updateRepository',
99-
new Error('Repository not found'),
100-
404,
105+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
106+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
107+
const mockSetupSdk = vi.mocked(setupSdk)
108+
const mockHandleApi = vi.mocked(handleApiCall)
109+
110+
const mockSdk = {
111+
updateRepository: vi.fn().mockRejectedValue(new Error('Repository not found')),
112+
}
113+
114+
mockSetupSdk.mockResolvedValue(createSuccessResult(mockSdk as any))
115+
mockHandleApi.mockResolvedValue(
116+
createErrorResult('Repository not found', { code: 404 }),
101117
)
102118

103119
const config = {
@@ -112,12 +128,14 @@ describe('fetchUpdateRepo', () => {
112128
const result = await fetchUpdateRepo(config)
113129

114130
expect(result.ok).toBe(false)
115-
expect(result.code).toBe(404)
131+
if (!result.ok) {
132+
expect(result.code).toBe(404)
133+
}
116134
})
117135

118136
it('passes custom SDK options', async () => {
119-
const { setupSdk } = await import('../../utils/socket/sdk.mts')
120-
const { handleApiCall } = await import('../../utils/socket/api.mts')
137+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
138+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
121139
const mockSetupSdk = vi.mocked(setupSdk)
122140
const mockHandleApi = vi.mocked(handleApiCall)
123141

@@ -148,8 +166,8 @@ describe('fetchUpdateRepo', () => {
148166
})
149167

150168
it('handles visibility changes', async () => {
151-
const { setupSdk } = await import('../../utils/socket/sdk.mts')
152-
const { handleApiCall } = await import('../../utils/socket/api.mts')
169+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
170+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
153171
const mockSetupSdk = vi.mocked(setupSdk)
154172
const mockHandleApi = vi.mocked(handleApiCall)
155173

@@ -186,8 +204,8 @@ describe('fetchUpdateRepo', () => {
186204
})
187205

188206
it('handles default branch updates', async () => {
189-
const { setupSdk } = await import('../../utils/socket/sdk.mts')
190-
const { handleApiCall } = await import('../../utils/socket/api.mts')
207+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
208+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
191209
const mockSetupSdk = vi.mocked(setupSdk)
192210
const mockHandleApi = vi.mocked(handleApiCall)
193211

@@ -224,8 +242,8 @@ describe('fetchUpdateRepo', () => {
224242
})
225243

226244
it('handles empty or minimal updates', async () => {
227-
const { setupSdk } = await import('../../utils/socket/sdk.mts')
228-
const { handleApiCall } = await import('../../utils/socket/api.mts')
245+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
246+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
229247
const mockSetupSdk = vi.mocked(setupSdk)
230248
const mockHandleApi = vi.mocked(handleApiCall)
231249

@@ -262,8 +280,8 @@ describe('fetchUpdateRepo', () => {
262280
})
263281

264282
it('uses null prototype for options', async () => {
265-
const { setupSdk } = await import('../../utils/socket/sdk.mts')
266-
const { handleApiCall } = await import('../../utils/socket/api.mts')
283+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
284+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
267285
const mockSetupSdk = vi.mocked(setupSdk)
268286
const mockHandleApi = vi.mocked(handleApiCall)
269287

0 commit comments

Comments
 (0)