Skip to content

Commit e780eac

Browse files
committed
fix(tests): use vi.importMock() consistently in fetch-create-repo tests
- Replace await import() with vi.importMock() for consistent mock access - Set up mocks directly instead of using helpers with module cache issues - Add beforeEach to clear mocks between tests - Import createErrorResult for error test cases - All 7 tests now pass
1 parent e0cd089 commit e780eac

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

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

Lines changed: 42 additions & 24 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 { fetchCreateRepo } from './fetch-create-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('fetchCreateRepo', () => {
19+
beforeEach(() => {
20+
vi.clearAllMocks()
21+
})
22+
2023
it('creates 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

@@ -69,10 +72,15 @@ describe('fetchCreateRepo', () => {
6972
})
7073

7174
it('handles SDK setup failure', async () => {
72-
await setupSdkSetupFailure('Failed to setup SDK', {
73-
code: 1,
74-
cause: 'Missing API token',
75-
})
75+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
76+
const mockSetupSdk = vi.mocked(setupSdk)
77+
78+
mockSetupSdk.mockResolvedValue(
79+
createErrorResult('Failed to setup SDK', {
80+
code: 1,
81+
cause: 'Missing API token',
82+
}),
83+
)
7684

7785
const result = await fetchCreateRepo({
7886
orgSlug: 'org',
@@ -87,10 +95,18 @@ describe('fetchCreateRepo', () => {
8795
})
8896

8997
it('handles API call failure', async () => {
90-
await setupSdkMockError(
91-
'createRepository',
92-
new Error('Repository already exists'),
93-
409,
98+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
99+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
100+
const mockSetupSdk = vi.mocked(setupSdk)
101+
const mockHandleApi = vi.mocked(handleApiCall)
102+
103+
const mockSdk = {
104+
createRepository: vi.fn().mockRejectedValue(new Error('Repository already exists')),
105+
}
106+
107+
mockSetupSdk.mockResolvedValue(createSuccessResult(mockSdk))
108+
mockHandleApi.mockResolvedValue(
109+
createErrorResult('Repository already exists', { code: 409 }),
94110
)
95111

96112
const result = await fetchCreateRepo({
@@ -103,12 +119,14 @@ describe('fetchCreateRepo', () => {
103119
})
104120

105121
expect(result.ok).toBe(false)
106-
expect(result.code).toBe(409)
122+
if (!result.ok) {
123+
expect(result.code).toBe(409)
124+
}
107125
})
108126

109127
it('passes custom SDK options', async () => {
110-
const { setupSdk } = await import('../../utils/socket/sdk.mts')
111-
const { handleApiCall } = await import('../../utils/socket/api.mts')
128+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
129+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
112130
const mockSetupSdk = vi.mocked(setupSdk)
113131
const mockHandleApi = vi.mocked(handleApiCall)
114132

@@ -140,8 +158,8 @@ describe('fetchCreateRepo', () => {
140158
})
141159

142160
it('handles minimal repository data', async () => {
143-
const { setupSdk } = await import('../../utils/socket/sdk.mts')
144-
const { handleApiCall } = await import('../../utils/socket/api.mts')
161+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
162+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
145163
const mockSetupSdk = vi.mocked(setupSdk)
146164
const mockHandleApi = vi.mocked(handleApiCall)
147165

@@ -171,8 +189,8 @@ describe('fetchCreateRepo', () => {
171189
})
172190

173191
it('handles full repository configuration', async () => {
174-
const { setupSdk } = await import('../../utils/socket/sdk.mts')
175-
const { handleApiCall } = await import('../../utils/socket/api.mts')
192+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
193+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
176194
const mockSetupSdk = vi.mocked(setupSdk)
177195
const mockHandleApi = vi.mocked(handleApiCall)
178196

@@ -204,8 +222,8 @@ describe('fetchCreateRepo', () => {
204222
})
205223

206224
it('uses null prototype for options', async () => {
207-
const { setupSdk } = await import('../../utils/socket/sdk.mts')
208-
const { handleApiCall } = await import('../../utils/socket/api.mts')
225+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
226+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
209227
const mockSetupSdk = vi.mocked(setupSdk)
210228
const mockHandleApi = vi.mocked(handleApiCall)
211229

0 commit comments

Comments
 (0)