Files
accounted/lib/supabase/__tests__/server.test.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

48 lines
1.5 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const { createServerClientMock, cookiesMock } = vi.hoisted(() => ({
createServerClientMock: vi.fn(),
cookiesMock: vi.fn(),
}))
vi.mock('@supabase/ssr', () => ({
createServerClient: createServerClientMock,
}))
vi.mock('next/headers', () => ({
cookies: cookiesMock,
}))
describe('createServiceClient', () => {
beforeEach(() => {
vi.resetModules()
vi.clearAllMocks()
vi.stubEnv('NEXT_PUBLIC_SUPABASE_URL', 'https://project.supabase.co')
vi.stubEnv('NEXT_PUBLIC_SUPABASE_ANON_KEY', 'anon-key')
vi.stubEnv('SUPABASE_SERVICE_ROLE_KEY', 'service-role-key')
})
afterEach(() => {
vi.unstubAllEnvs()
})
it('uses the service-role key with a cookie-free client', async () => {
const serviceClient = { kind: 'service' }
createServerClientMock.mockReturnValue(serviceClient)
const { createServiceClient } = await import('../server')
expect(createServiceClient()).toBe(serviceClient)
expect(createServerClientMock).toHaveBeenCalledWith(
'https://project.supabase.co',
'service-role-key',
expect.objectContaining({ cookies: expect.any(Object) }),
)
const options = createServerClientMock.mock.calls[0][2] as {
cookies: { getAll: () => unknown[]; setAll: () => void }
}
expect(options.cookies.getAll()).toEqual([])
expect(() => options.cookies.setAll()).not.toThrow()
expect(cookiesMock).not.toHaveBeenCalled()
})
})