ed43399b24
- Install @types/bun and @types/jsdom for proper type definitions - Fix mock function typing in test files - Create jest-dom.d.ts to properly extend Bun's Matchers interface - Remove MockedFunction imports (not supported in Bun's types) - Cast global.fetch and useSession mocks to any where needed - Fix mock.module return types to match expected signatures This resolves TypeScript errors that were preventing proper code completion and type checking in IDEs.
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import { describe, it, expect, mock, beforeEach } from 'bun:test'
|
|
import { getSession } from '@/lib/auth-simple'
|
|
|
|
// Mock next/headers
|
|
mock.module('next/headers', () => ({
|
|
cookies: mock(() => Promise.resolve({
|
|
get: mock(() => ({ name: 'better-auth.session_token', value: 'test-token' })),
|
|
})),
|
|
headers: mock(() => Promise.resolve({
|
|
get: mock(() => null),
|
|
})),
|
|
}))
|
|
|
|
// Mock fetch
|
|
const mockFetch = mock(async () => new Response())
|
|
global.fetch = mockFetch as any
|
|
|
|
describe('getSession', () => {
|
|
beforeEach(() => {
|
|
mock.clearAllMocks()
|
|
})
|
|
|
|
it('returns session data when auth API returns 200', async () => {
|
|
const mockSession = {
|
|
session: { token: 'test-token', expiresAt: new Date().toISOString() },
|
|
user: { id: 'user-123', email: 'test@example.com' },
|
|
}
|
|
|
|
mockFetch.mockImplementation(async () =>
|
|
new Response(JSON.stringify(mockSession), { status: 200 })
|
|
)
|
|
|
|
const result = await getSession()
|
|
|
|
expect(result).toEqual(mockSession)
|
|
expect(mockFetch).toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns null when auth API returns non-200 status', async () => {
|
|
mockFetch.mockImplementation(async () =>
|
|
new Response(null, { status: 401 })
|
|
)
|
|
|
|
const result = await getSession()
|
|
|
|
expect(result).toBeNull()
|
|
})
|
|
|
|
it('returns null when an error occurs', async () => {
|
|
mockFetch.mockRejectedValue(new Error('Network error'))
|
|
|
|
const result = await getSession()
|
|
|
|
expect(result).toBeNull()
|
|
})
|
|
})
|