-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjest.setup.ts
More file actions
71 lines (63 loc) · 1.56 KB
/
jest.setup.ts
File metadata and controls
71 lines (63 loc) · 1.56 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
import '@testing-library/jest-dom'
// Polyfill setImmediate for jsdom (used by yauzl zip library)
if (typeof globalThis.setImmediate === 'undefined') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).setImmediate = (fn: (...args: unknown[]) => void, ...args: unknown[]) => setTimeout(fn, 0, ...args);
}
// Mock Next.js App Router navigation
jest.mock('next/navigation', () => ({
useRouter() {
return {
push: jest.fn(),
replace: jest.fn(),
prefetch: jest.fn(),
back: jest.fn(),
forward: jest.fn(),
refresh: jest.fn(),
}
},
usePathname() {
return '/'
},
useSearchParams() {
return new URLSearchParams()
},
useParams() {
return {}
},
}))
// Mock Next.js Link
jest.mock('next/link', () => {
return ({ children }: { children: React.ReactNode }) => {
return children
}
})
// Mock UUID
jest.mock('uuid', () => ({
v4: () => 'test-uuid-123'
}))
// Mock Next.js server components
class MockNextRequest {
url: string;
method: string;
headers: Headers;
nextUrl: URL;
constructor(url: string, init?: { method?: string; headers?: HeadersInit }) {
this.url = url;
this.method = init?.method || 'GET';
this.headers = new Headers(init?.headers);
this.nextUrl = new URL(url);
}
}
const MockNextResponse = {
json: (data: unknown, init?: { status?: number }) => {
return {
json: async () => data,
status: init?.status || 200,
};
},
};
jest.mock('next/server', () => ({
NextRequest: MockNextRequest,
NextResponse: MockNextResponse,
}))