-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvitestSetup.ts
49 lines (43 loc) · 1.37 KB
/
vitestSetup.ts
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
import { useState } from 'react'
import { vi } from 'vitest'
process.env.MOCK_PATHNAME = '/'
process.env.API_ENDPOINT = 'http://api-endpoint'
process.env.API_KEY = 'test-api-key'
vi.mock('react', async () => ({
...(await vi.importActual('react')),
useContext: () => undefined,
useActionState: (_action: () => unknown, initialState: unknown) => {
const [isPending, setIsPending] = useState(false)
return [initialState, vi.fn(() => setIsPending(true)), isPending]
},
}))
vi.mock('next/navigation', async () => ({
...(await vi.importActual('next/navigation')),
useRouter: vi.fn(() => ({
push: vi.fn(),
back: vi.fn(),
})),
notFound: vi.fn(() => {
throw new Error('Mock notFound error')
}),
redirect: vi.fn(() => {
throw new Error('Mock redirect error')
}),
useSearchParams: () => new URLSearchParams(process.env.MOCK_PATHNAME),
usePathname: () => process.env.MOCK_PATHNAME,
}))
vi.mock('next/headers', async () => {
const cookies: Record<string, string> = {}
return {
...(await vi.importActual('next/headers')),
cookies: vi.fn(() => ({
set: vi.fn(({ name, value }: { name: string; value: string }) => {
cookies[name] = value
}),
get: vi.fn((name: string) =>
cookies[name] ? { name, value: cookies[name] } : undefined,
),
delete: vi.fn((name: string) => delete cookies[name]),
})),
}
})