-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
85 lines (78 loc) · 2.49 KB
/
vitest.setup.ts
File metadata and controls
85 lines (78 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import '@testing-library/jest-dom'
import { cleanup } from '@testing-library/react'
import { afterEach, beforeEach, vi } from 'vitest'
import React from 'react'
// Setup environment variables before each test
beforeEach(() => {
vi.stubEnv('NEXT_PUBLIC_SUPABASE_URL', 'https://test.supabase.co')
vi.stubEnv('NEXT_PUBLIC_SUPABASE_ANON_KEY', 'test-key')
vi.stubEnv('NEXT_PUBLIC_APP_URL', 'http://localhost:3000')
vi.stubEnv('NEXT_PUBLIC_APP_DOMAIN', 'localhost')
vi.stubEnv('NEXT_PUBLIC_APP_EMAIL', '@test.com')
})
// Cleanup after each test
afterEach(() => {
cleanup()
// Restore real timers FIRST — before restoreAllMocks() — so that any mock
// wrapping timer globals is torn down while the fake-timer system is still
// coherent. Reversing this order can leave Vitest's fake-timer bookkeeping
// in a corrupt state and cause the next test's imports / async ops to hang.
vi.useRealTimers()
vi.restoreAllMocks()
vi.unstubAllEnvs()
})
// Mock window.matchMedia (not available in jsdom)
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
})
// Mock Next.js router
vi.mock('next/navigation', () => ({
useRouter: () => ({
push: vi.fn(),
replace: vi.fn(),
prefetch: vi.fn(),
back: vi.fn(),
forward: vi.fn(),
refresh: vi.fn(),
pathname: '/',
query: {},
asPath: '/',
}),
usePathname: () => '/',
useSearchParams: () => new URLSearchParams(),
useParams: () => ({}),
}))
// Mock Next.js Image
vi.mock('next/image', () => ({
default: ({ alt, ...props }: any) => {
return React.createElement('img', { alt, ...props })
},
}))
// Mock Supabase client
vi.mock('@/lib/supabase/client', () => ({
createClient: vi.fn(() => ({
auth: {
getUser: vi.fn(() => Promise.resolve({ data: { user: null }, error: null })),
getSession: vi.fn(() => Promise.resolve({ data: { session: null }, error: null })),
signOut: vi.fn(() => Promise.resolve({ error: null })),
},
from: vi.fn(() => ({
select: vi.fn().mockReturnThis(),
insert: vi.fn().mockReturnThis(),
update: vi.fn().mockReturnThis(),
delete: vi.fn().mockReturnThis(),
eq: vi.fn().mockReturnThis(),
single: vi.fn(() => Promise.resolve({ data: null, error: null })),
})),
})),
}))