-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
202 lines (176 loc) · 4.69 KB
/
Copy pathjest.setup.js
File metadata and controls
202 lines (176 loc) · 4.69 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Learn more: https://github.com/testing-library/jest-dom
import "@testing-library/jest-dom";
// Mock Next.js router
jest.mock("next/router", () => ({
useRouter() {
return {
route: "/",
pathname: "/",
query: {},
asPath: "/",
push: jest.fn(),
pop: jest.fn(),
reload: jest.fn(),
back: jest.fn(),
prefetch: jest.fn().mockResolvedValue(undefined),
beforePopState: jest.fn(),
events: {
on: jest.fn(),
off: jest.fn(),
emit: jest.fn(),
},
isFallback: false,
};
},
}));
// Mock Next.js 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(),
};
},
useSearchParams() {
return new URLSearchParams();
},
usePathname() {
return "/";
},
}));
// Mock crypto for UUID generation
if (!global.crypto) {
global.crypto = {
randomUUID: () => "test-uuid-1234-5678-9012-345678901234",
};
}
// Mock fetch globally
global.fetch = jest.fn();
// Mock Request constructor
global.Request = class Request {
constructor(url, init = {}) {
this.url = url;
this.method = init.method || "GET";
this.headers = new Map(Object.entries(init.headers || {}));
this.body = init.body;
}
};
// Mock console methods to reduce noise in tests
global.console = {
...console,
// Suppress all console output during tests to reduce noise
log: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
// error is handled separately below
};
// Mock environment variables
process.env.NODE_ENV = "test";
process.env.NEXT_PUBLIC_SANITY_PROJECT_ID = "test-project-id";
process.env.NEXT_PUBLIC_SANITY_DATASET = "test";
process.env.SANITY_API_TOKEN = "test-token";
process.env.STRIPE_SECRET_KEY = "test-stripe-key";
process.env.STRIPE_WEBHOOK_SECRET = "test-webhook-secret";
process.env.EMAIL_SERVER_HOST = "test-host";
process.env.EMAIL_SERVER_PORT = "587";
process.env.EMAIL_SERVER_USER = "test-user";
process.env.EMAIL_SERVER_PASSWORD = "test-password";
process.env.EMAIL_FROM = "test@example.com";
// Mock ResizeObserver
global.ResizeObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}));
// Mock IntersectionObserver
global.IntersectionObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}));
// Mock matchMedia
Object.defineProperty(window, "matchMedia", {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
// Mock window.scrollTo
Object.defineProperty(window, "scrollTo", {
writable: true,
value: jest.fn(),
});
// Mock window.alert
Object.defineProperty(window, "alert", {
writable: true,
value: jest.fn(),
});
// Mock window.confirm
Object.defineProperty(window, "confirm", {
writable: true,
value: jest.fn(),
});
// Mock window.prompt
Object.defineProperty(window, "prompt", {
writable: true,
value: jest.fn(),
});
// Mock localStorage
const localStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
};
global.localStorage = localStorageMock;
// Mock sessionStorage
const sessionStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
};
global.sessionStorage = sessionStorageMock;
// Mock URL.createObjectURL
global.URL.createObjectURL = jest.fn(() => "mocked-url");
// Mock URL.revokeObjectURL
global.URL.revokeObjectURL = jest.fn();
// Mock requestAnimationFrame
global.requestAnimationFrame = jest.fn(cb => setTimeout(cb, 0));
// Mock cancelAnimationFrame
global.cancelAnimationFrame = jest.fn();
// Mock performance.now
global.performance.now = jest.fn(() => 1234567890);
// Mock getComputedStyle
Object.defineProperty(window, "getComputedStyle", {
value: () => ({
getPropertyValue: () => "",
}),
});
// Mock Element.prototype methods
Element.prototype.scrollIntoView = jest.fn();
Element.prototype.scrollTo = jest.fn();
// Mock HTMLElement.prototype methods
HTMLElement.prototype.focus = jest.fn();
HTMLElement.prototype.blur = jest.fn();
HTMLElement.prototype.click = jest.fn();
// Mock console.error to fail tests on React warnings
const originalError = console.error;
beforeAll(() => {
console.error = jest.fn(); // Completely silence console.error during tests
});
afterAll(() => {
console.error = originalError;
});