[test] Fix axios mocking in firebaseAuthStore tests

Use vi.mock() with factory function to ensure axios mock is set up before module imports. This fixes test failures caused by the store creating axios client at module level.

Fixes test execution in CI
This commit is contained in:
bymyself
2025-08-15 15:37:06 -07:00
parent 81ddfc2f58
commit 2e80a5789c

View File

@@ -7,23 +7,28 @@ import * as vuefire from 'vuefire'
import { useFirebaseAuthStore } from '@/stores/firebaseAuthStore'
// Mock axios before any imports that use it
vi.mock('axios')
vi.mock('axios', () => {
const mockAxiosInstance = {
get: vi.fn().mockResolvedValue({
data: { balance: { credits: 0 } },
status: 200
}),
post: vi.fn().mockResolvedValue({
data: { id: 'test-customer-id' },
status: 201
})
}
return {
default: {
create: vi.fn().mockReturnValue(mockAxiosInstance),
isAxiosError: vi.fn().mockImplementation(() => false)
}
}
})
const mockedAxios = vi.mocked(axios)
// Set up axios mock with default instance
const mockAxiosInstance = {
get: vi.fn().mockResolvedValue({
data: { balance: { credits: 0 } },
status: 200
}),
post: vi.fn().mockResolvedValue({
data: { id: 'test-customer-id' },
status: 201
})
}
mockedAxios.create = vi.fn().mockReturnValue(mockAxiosInstance)
mockedAxios.isAxiosError = vi.fn().mockImplementation(() => false) as any
const mockAxiosInstance = mockedAxios.create() as any
// Mock fetch
const mockFetch = vi.fn()
@@ -122,7 +127,7 @@ describe('useFirebaseAuthStore', () => {
data: { id: 'test-customer-id' },
status: 201
})
mockedAxios.isAxiosError = vi.fn().mockImplementation(() => false) as any
;(mockedAxios.isAxiosError as any).mockReturnValue(false)
// Mock useFirebaseAuth to return our mock auth object
vi.mocked(vuefire.useFirebaseAuth).mockReturnValue(mockAuth as any)