Skip to content

Commit a31546e

Browse files
committed
fix(tests): replace await import with vi.importMock in fetch-threat-feed tests
- Add beforeEach hook to clear mocks between tests - Replace all `await import()` with `vi.importMock()` for mocked dependencies - Ensures consistent mock state across all test cases All 8 tests in fetch-threat-feed.test.mts now pass.
1 parent dfa0d8b commit a31546e

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

packages/cli/src/commands/threat-feed/fetch-threat-feed.test.mts

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

33
import { fetchThreatFeed } from './fetch-threat-feed.mts'
44
import {
@@ -12,8 +12,12 @@ vi.mock('../../utils/socket/api.mts', () => ({
1212
}))
1313

1414
describe('fetchThreatFeed', () => {
15+
beforeEach(() => {
16+
vi.clearAllMocks()
17+
})
18+
1519
it('fetches threat feed successfully', async () => {
16-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
20+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
1721
const mockQueryApi = vi.mocked(queryApiSafeJson)
1822

1923
const mockData = {
@@ -61,7 +65,7 @@ describe('fetchThreatFeed', () => {
6165
})
6266

6367
it('handles SDK setup failure', async () => {
64-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
68+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
6569
const mockQueryApi = vi.mocked(queryApiSafeJson)
6670

6771
const error = createErrorResult('Failed to fetch threat feed', {
@@ -85,7 +89,7 @@ describe('fetchThreatFeed', () => {
8589
})
8690

8791
it('handles API call failure', async () => {
88-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
92+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
8993
const mockQueryApi = vi.mocked(queryApiSafeJson)
9094

9195
mockQueryApi.mockResolvedValue(
@@ -108,7 +112,7 @@ describe('fetchThreatFeed', () => {
108112
})
109113

110114
it('passes custom SDK options', async () => {
111-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
115+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
112116
const mockQueryApi = vi.mocked(queryApiSafeJson)
113117

114118
mockQueryApi.mockResolvedValue(createSuccessResult({}))
@@ -131,7 +135,7 @@ describe('fetchThreatFeed', () => {
131135
})
132136

133137
it('handles filtering by severity levels', async () => {
134-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
138+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
135139
const mockQueryApi = vi.mocked(queryApiSafeJson)
136140

137141
mockQueryApi.mockResolvedValue(createSuccessResult({ threats: [] }))
@@ -154,7 +158,7 @@ describe('fetchThreatFeed', () => {
154158
})
155159

156160
it('handles pagination parameters', async () => {
157-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
161+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
158162
const mockQueryApi = vi.mocked(queryApiSafeJson)
159163

160164
mockQueryApi.mockResolvedValue(createSuccessResult({ threats: [] }))
@@ -177,7 +181,7 @@ describe('fetchThreatFeed', () => {
177181
})
178182

179183
it('handles date range filtering', async () => {
180-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
184+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
181185
const mockQueryApi = vi.mocked(queryApiSafeJson)
182186

183187
mockQueryApi.mockResolvedValue(createSuccessResult({ threats: [] }))
@@ -200,7 +204,7 @@ describe('fetchThreatFeed', () => {
200204
})
201205

202206
it('uses null prototype for options', async () => {
203-
const { queryApiSafeJson } = await import('../../utils/socket/api.mts')
207+
const { queryApiSafeJson } = await vi.importMock('../../utils/socket/api.mts')
204208
const mockQueryApi = vi.mocked(queryApiSafeJson)
205209

206210
mockQueryApi.mockResolvedValue(createSuccessResult({}))

0 commit comments

Comments
 (0)