-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.ts
More file actions
101 lines (91 loc) · 2.95 KB
/
Copy pathjest.setup.ts
File metadata and controls
101 lines (91 loc) · 2.95 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import "@testing-library/jest-dom";
import { TextEncoder, TextDecoder } from "util";
// Polyfill TextEncoder/TextDecoder for jsdom environment
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder as typeof global.TextDecoder;
// Mock @upstash/redis before any imports that use it
jest.mock("@upstash/redis", () => ({
Redis: jest.fn().mockImplementation(() => ({
ping: jest.fn().mockResolvedValue("PONG"),
get: jest.fn().mockResolvedValue(null),
set: jest.fn().mockResolvedValue("OK"),
del: jest.fn().mockResolvedValue(1),
})),
}));
// Mock @upstash/ratelimit
jest.mock("@upstash/ratelimit", () => ({
Ratelimit: jest.fn().mockImplementation(() => ({
limit: jest.fn().mockResolvedValue({
success: true,
limit: 100,
remaining: 99,
reset: Date.now() + 60000
}),
})),
}));
// Mock next/cache to avoid server-only code in tests
jest.mock("next/cache", () => ({
revalidatePath: jest.fn(),
revalidateTag: jest.fn(),
unstable_cache: jest.fn((fn) => fn),
}));
// Mock Redis and cache modules to avoid ESM import issues
jest.mock("@/lib/redis", () => ({
redis: null,
isRedisAvailable: jest.fn().mockResolvedValue(false),
getRedisHealth: jest.fn().mockResolvedValue({ status: "unavailable" }),
}));
jest.mock("@/lib/cache", () => ({
cached: jest.fn((key, fn) => fn()),
invalidateCache: jest.fn(),
invalidateUserCache: jest.fn(),
invalidateGroupCache: jest.fn(),
}));
jest.mock("@/lib/distributed-lock", () => ({
withLock: jest.fn((key, fn) => fn()),
LockKeys: {
settlement: jest.fn((id) => `lock:settlement:${id}`),
expense: jest.fn((id) => `lock:expense:${id}`),
},
}));
// Mock Next.js router
jest.mock("next/navigation", () => ({
useRouter: () => ({
push: jest.fn(),
replace: jest.fn(),
refresh: jest.fn(),
back: jest.fn(),
forward: jest.fn(),
prefetch: jest.fn(),
}),
usePathname: () => "/",
useSearchParams: () => new URLSearchParams(),
}));
// Mock Supabase client
jest.mock("@/lib/supabase/client", () => ({
createClient: () => ({
auth: {
getUser: jest.fn(),
signInWithPassword: jest.fn(),
signUp: jest.fn(),
signOut: jest.fn(),
updateUser: jest.fn(),
},
from: jest.fn(() => ({
select: jest.fn().mockReturnThis(),
insert: jest.fn().mockReturnThis(),
update: jest.fn().mockReturnThis(),
delete: jest.fn().mockReturnThis(),
eq: jest.fn().mockReturnThis(),
single: jest.fn(),
})),
storage: {
from: jest.fn(() => ({
upload: jest.fn(),
remove: jest.fn(),
list: jest.fn(),
getPublicUrl: jest.fn(() => ({ data: { publicUrl: "https://example.com/avatar.jpg" } })),
})),
},
}),
}));