test: add vitest test infrastructure and configuration

- Add vitest.config.ts with coverage thresholds
- Add test/setup.ts with testing-library setup
- Add test/helpers.ts with test utilities
This commit is contained in:
Millian Lamiaux
2026-03-14 20:42:22 +01:00
parent 52429d957f
commit 592d04e170
3 changed files with 253 additions and 0 deletions

59
admin-web/test/setup.ts Normal file
View File

@@ -0,0 +1,59 @@
import '@testing-library/jest-dom'
import { vi, beforeAll, afterAll, beforeEach } from 'vitest'
// Check if we have test database credentials
const hasTestDb = process.env.NEXT_PUBLIC_SUPABASE_URL &&
(process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY)
// Global test setup
beforeAll(async () => {
if (hasTestDb) {
console.log('🧪 Setting up test environment with real database...')
const testUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
if (!testUrl?.includes('test') && !testUrl?.includes('localhost')) {
console.warn('⚠️ WARNING: Not using a test database!')
console.warn('Current URL:', testUrl)
}
} else {
console.log('🧪 Running tests with mocked database...')
}
})
beforeEach(async () => {
// Cleanup before each test to ensure isolation (only if we have real DB)
if (hasTestDb) {
try {
const { cleanupTestData } = await import('./helpers')
await cleanupTestData()
} catch (e) {
// Silently ignore if helpers not available
}
}
})
afterAll(async () => {
if (hasTestDb) {
console.log('🧹 Cleaning up test environment...')
try {
const { cleanupTestData } = await import('./helpers')
await cleanupTestData()
} catch (e) {
// Silently ignore if helpers not available
}
}
})
// Mock Supabase client for component tests that don't need real DB
vi.mock('@/lib/supabase', () => ({
supabase: {
storage: {
from: vi.fn(() => ({
upload: vi.fn(),
remove: vi.fn(),
move: vi.fn(),
list: vi.fn(),
getPublicUrl: vi.fn(() => ({ data: { publicUrl: 'https://example.com/test.jpg' } })),
})),
},
},
}))