Skip to content

Commit dfa0d8b

Browse files
committed
fix(tests): replace helper functions with direct mocks in fetch-list-repos and fetch-list-all-repos
- Replace setupSdkSetupFailure with direct createErrorResult mocks - Replace setupSdkMockError with direct mock setup - Add createErrorResult import for error cases - Use vi.importMock() consistently for all mocked dependencies - Wrap result.code checks with !result.ok type guard All tests now pass (16 total tests across both files).
1 parent da559c8 commit dfa0d8b

File tree

2 files changed

+60
-24
lines changed

2 files changed

+60
-24
lines changed

packages/cli/src/commands/repository/fetch-list-all-repos.test.mts

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

33
import { fetchListAllRepos } from './fetch-list-all-repos.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', () => ({
@@ -62,23 +61,42 @@ describe('fetchListAllRepos', () => {
6261
})
6362

6463
it('handles SDK setup failure', async () => {
65-
await setupSdkSetupFailure('Failed to setup SDK', {
66-
code: 1,
67-
cause: 'Missing API token',
68-
})
64+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
65+
const mockSetupSdk = vi.mocked(setupSdk)
66+
67+
mockSetupSdk.mockResolvedValue(
68+
createErrorResult('Failed to setup SDK', {
69+
code: 1,
70+
cause: 'Missing API token',
71+
}),
72+
)
6973

7074
const result = await fetchListAllRepos('org')
7175

7276
expect(result.ok).toBe(false)
7377
})
7478

7579
it('handles API call failure', async () => {
76-
await setupSdkMockError('listRepositories', new Error('Access denied'), 403)
80+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
81+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
82+
const mockSetupSdk = vi.mocked(setupSdk)
83+
const mockHandleApi = vi.mocked(handleApiCall)
84+
85+
const mockSdk = {
86+
listRepositories: vi.fn().mockRejectedValue(new Error('Access denied')),
87+
}
88+
89+
mockSetupSdk.mockResolvedValue(createSuccessResult(mockSdk))
90+
mockHandleApi.mockResolvedValue(
91+
createErrorResult('Access denied', { code: 403 }),
92+
)
7793

7894
const result = await fetchListAllRepos('private-org')
7995

8096
expect(result.ok).toBe(false)
81-
expect(result.code).toBe(403)
97+
if (!result.ok) {
98+
expect(result.code).toBe(403)
99+
}
82100
})
83101

84102
it('handles multiple pages of repositories', async () => {

packages/cli/src/commands/repository/fetch-list-repos.test.mts

Lines changed: 32 additions & 14 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 { fetchListRepos } from './fetch-list-repos.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,6 +16,10 @@ vi.mock('../../utils/socket/sdk.mts', () => ({
1716
}))
1817

1918
describe('fetchListRepos', () => {
19+
beforeEach(() => {
20+
vi.clearAllMocks()
21+
})
22+
2023
it('lists repositories with pagination successfully', async () => {
2124
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
2225
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
@@ -70,10 +73,15 @@ describe('fetchListRepos', () => {
7073
})
7174

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

7886
const config = {
7987
direction: 'asc',
@@ -89,10 +97,18 @@ describe('fetchListRepos', () => {
8997
})
9098

9199
it('handles API call failure', async () => {
92-
await setupSdkMockError(
93-
'listRepositories',
94-
new Error('Invalid page number'),
95-
400,
100+
const { setupSdk } = await vi.importMock('../../utils/socket/sdk.mts')
101+
const { handleApiCall } = await vi.importMock('../../utils/socket/api.mts')
102+
const mockSetupSdk = vi.mocked(setupSdk)
103+
const mockHandleApi = vi.mocked(handleApiCall)
104+
105+
const mockSdk = {
106+
listRepositories: vi.fn().mockRejectedValue(new Error('Invalid page number')),
107+
}
108+
109+
mockSetupSdk.mockResolvedValue(createSuccessResult(mockSdk))
110+
mockHandleApi.mockResolvedValue(
111+
createErrorResult('Invalid page number', { code: 400 }),
96112
)
97113

98114
const config = {
@@ -106,7 +122,9 @@ describe('fetchListRepos', () => {
106122
const result = await fetchListRepos(config)
107123

108124
expect(result.ok).toBe(false)
109-
expect(result.code).toBe(400)
125+
if (!result.ok) {
126+
expect(result.code).toBe(400)
127+
}
110128
})
111129

112130
it('passes custom SDK options', async () => {

0 commit comments

Comments
 (0)