forked from zaidmukaddam/scira-mcp-chat
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathjest.setup.js
More file actions
288 lines (256 loc) · 6.92 KB
/
Copy pathjest.setup.js
File metadata and controls
288 lines (256 loc) · 6.92 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
if (typeof window !== 'undefined') {
require('@testing-library/jest-dom');
// jsdom ships requestSubmit but throws "Not implemented" — always override for Enter-to-submit tests
if (typeof HTMLFormElement !== 'undefined') {
HTMLFormElement.prototype.requestSubmit = function requestSubmit(submitter) {
if (submitter) {
submitter.click();
return;
}
const event = new Event('submit', { bubbles: true, cancelable: true });
this.dispatchEvent(event);
};
}
// React 19 and modern DOM feature support
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 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(),
}));
}
// React 19 specific mocks and polyfills
// Mock structuredClone for React 19 compatibility
if (typeof global.structuredClone !== 'function') {
global.structuredClone = function structuredClone(value) {
if (value === null || value === undefined) {
return value;
}
try {
// For objects and arrays, use JSON methods
if (typeof value === 'object') {
return JSON.parse(JSON.stringify(value));
}
// For primitive values, return directly
return value;
} catch (error) {
console.warn('structuredClone polyfill failed:', error);
// Returns a shallow copy as fallback
return Array.isArray(value) ? [...value] : { ...value };
}
};
}
// Mock modern browser APIs that React 19 might use
global.Request = global.Request || class Request {};
global.Response = global.Response || class Response {
static json(data, init) {
return new Response(JSON.stringify(data), {
...init,
headers: {
'Content-Type': 'application/json',
...init?.headers,
},
});
}
};
global.Headers = global.Headers || class Headers {
constructor(init) {
this._headers = new Map();
if (init) {
Object.entries(init).forEach(([key, value]) => {
this.set(key, value);
});
}
}
get(name) {
return this._headers.get(name.toLowerCase());
}
set(name, value) {
this._headers.set(name.toLowerCase(), value);
}
has(name) {
return this._headers.has(name.toLowerCase());
}
delete(name) {
this._headers.delete(name.toLowerCase());
}
entries() {
return this._headers.entries();
}
keys() {
return this._headers.keys();
}
values() {
return this._headers.values();
}
};
// Mock Next.js edge runtime cookies for NextRequest
jest.mock('next/dist/compiled/@edge-runtime/cookies', () => ({
RequestCookies: class RequestCookies {
constructor() {
this._cookies = new Map();
}
get(name) {
return this._cookies.get(name);
}
set(name, value) {
this._cookies.set(name, value);
}
delete(name) {
this._cookies.delete(name);
}
has(name) {
return this._cookies.has(name);
}
clear() {
this._cookies.clear();
}
},
ResponseCookies: class ResponseCookies {
constructor() {
this._cookies = new Map();
}
get(name) {
return this._cookies.get(name);
}
set(name, value) {
this._cookies.set(name, value);
}
delete(name) {
this._cookies.delete(name);
}
}
}));
// Silence specific React 19 warnings in tests
const originalError = console.error;
console.error = (...args) => {
// Suppress specific React 19 warnings during testing
if (typeof args[0] === 'string' &&
(args[0].includes('outdated JSX transform') ||
args[0].includes('Warning:') && args[0].includes('act'))) {
return;
}
originalError.call(console, ...args);
};
// 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 framer-motion
jest.mock('framer-motion', () => ({
motion: {
div: ({ children, ...props }) => <div {...props}>{children}</div>,
button: ({ children, ...props }) => <button {...props}>{children}</button>,
},
AnimatePresence: ({ children }) => <>{children}</>,
}));
// Mock motion/react
jest.mock('motion/react', () => ({
motion: {
div: ({ children, ...props }) => <div {...props}>{children}</div>,
button: ({ children, ...props }) => <button {...props}>{children}</button>,
},
AnimatePresence: ({ children }) => <>{children}</>,
}));
// Mock CSS modules - handled in moduleNameMapper in jest.config.js
// Mock next/image
jest.mock('next/image', () => ({
__esModule: true,
default: (props) => <img {...props} />,
}));
// Mock TextDecoder/TextEncoder and streaming APIs for Node.js environment
const { TextDecoder, TextEncoder } = require('util');
global.TextDecoder = TextDecoder;
global.TextEncoder = TextEncoder;
// Mock streaming APIs
global.TransformStream = class TransformStream {
constructor() {
this.readable = {};
this.writable = {};
}
};
global.ReadableStream = class ReadableStream {
constructor() {}
};
global.WritableStream = class WritableStream {
constructor() {}
};
// Mock database dependencies
jest.mock('drizzle-orm/neon-serverless', () => ({
drizzle: jest.fn(() => ({
select: jest.fn().mockReturnValue({
from: jest.fn().mockReturnValue({
where: jest.fn().mockReturnValue([]),
}),
}),
insert: jest.fn().mockReturnValue({
values: jest.fn().mockReturnValue({
returning: jest.fn().mockResolvedValue([]),
}),
}),
})),
}));
jest.mock('@neondatabase/serverless', () => ({
Pool: jest.fn(),
}));
// Mock nanoid
jest.mock('nanoid', () => ({
nanoid: jest.fn(() => 'test-id-123'),
}));
// Mock AI SDK providers
jest.mock('@ai-sdk/openai', () => ({
createOpenAI: jest.fn(() => ({})),
}));
jest.mock('@ai-sdk/anthropic', () => ({
createAnthropic: jest.fn(() => ({})),
}));
jest.mock('@ai-sdk/groq', () => ({
createGroq: jest.fn(() => ({})),
}));
jest.mock('@ai-sdk/xai', () => ({
createXai: jest.fn(() => ({})),
}));
jest.mock('@openrouter/ai-sdk-provider', () => ({
createOpenRouter: jest.fn(() => ({})),
}));
jest.mock('@requesty/ai-sdk', () => ({
createRequesty: jest.fn(() => ({})),
}));