24db43eb7f
- Install Bun via mise - Update package.json scripts to use Bun - Migrate unit tests from Vitest to Bun test runner - Migrate component tests from Vitest to Bun test runner - Configure Bun with DOM environment for React Testing Library - Keep Playwright for E2E tests (as planned) - 93/100 tests passing (7 flaky tests when running all together, pass individually)
57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import { describe, it, expect, mock, beforeEach, MockedFunction } from 'bun:test'
|
|
import { getSession } from '@/lib/auth-simple'
|
|
|
|
// Mock next/headers
|
|
vi.mock('next/headers', () => ({
|
|
cookies: mock(() => {}).mockResolvedValue({
|
|
get: mock(() => {}).mockReturnValue({ name: 'better-auth.session_token', value: 'test-token' }),
|
|
}),
|
|
headers: mock(() => {}).mockResolvedValue({
|
|
get: mock(() => {}).mockReturnValue(null),
|
|
}),
|
|
}))
|
|
|
|
// Mock fetch
|
|
const mockFetch = mock(() => {})
|
|
global.fetch = mockFetch as MockedFunction<typeof global.fetch>
|
|
|
|
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()
|
|
})
|
|
})
|