-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjest.setup.js
More file actions
599 lines (553 loc) · 16.5 KB
/
Copy pathjest.setup.js
File metadata and controls
599 lines (553 loc) · 16.5 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import '@testing-library/jest-dom'
import { TextDecoder, TextEncoder } from 'util'
// Polyfills for Node.js environment
global.TextEncoder = TextEncoder
global.TextDecoder = TextDecoder
// Mock Headers class
global.Headers = class MockHeaders extends Map {
constructor(init) {
super()
if (init) {
if (init instanceof Headers || init instanceof Map) {
for (const [key, value] of init) {
this.set(key, value)
}
} else if (typeof init === 'object') {
for (const [key, value] of Object.entries(init)) {
this.set(key, value)
}
}
}
}
get(name) {
return super.get(name.toLowerCase()) || null
}
set(name, value) {
return super.set(name.toLowerCase(), String(value))
}
has(name) {
return super.has(name.toLowerCase())
}
delete(name) {
return super.delete(name.toLowerCase())
}
append(name, value) {
const existing = this.get(name)
if (existing) {
this.set(name, `${existing}, ${value}`)
} else {
this.set(name, value)
}
}
}
// Mock Request for Next.js API routes (Web API compatible)
global.Request = class MockRequest {
constructor(input, init = {}) {
const url = typeof input === 'string' ? input : input.url
// Use defineProperties to make url and other properties read-only (like real Request)
Object.defineProperties(this, {
url: { value: url, enumerable: true },
method: { value: init?.method || 'GET', enumerable: true },
headers: { value: new Headers(init?.headers || {}), enumerable: true },
body: { value: init?.body || null, writable: true },
bodyUsed: { value: false, writable: true },
cache: { value: init?.cache || 'default', enumerable: true },
credentials: {
value: init?.credentials || 'same-origin',
enumerable: true,
},
destination: { value: '', enumerable: true },
integrity: { value: init?.integrity || '', enumerable: true },
mode: { value: init?.mode || 'cors', enumerable: true },
redirect: { value: init?.redirect || 'follow', enumerable: true },
referrer: { value: init?.referrer || 'about:client', enumerable: true },
referrerPolicy: { value: init?.referrerPolicy || '', enumerable: true },
})
}
clone() {
return new MockRequest(this.url, {
method: this.method,
headers: this.headers,
body: this.body,
})
}
async json() {
if (typeof this.body === 'string') {
return JSON.parse(this.body)
}
return this.body || {}
}
async text() {
return String(this.body || '')
}
async arrayBuffer() {
return new ArrayBuffer(0)
}
async blob() {
return new Blob([this.body || ''])
}
async formData() {
return new FormData()
}
}
// Mock Response for Next.js API routes (Web API compatible with static json method)
global.Response = class MockResponse {
constructor(body, init = {}) {
Object.defineProperties(this, {
body: { value: body, enumerable: true },
status: { value: init?.status || 200, enumerable: true },
statusText: { value: init?.statusText || 'OK', enumerable: true },
headers: { value: new Headers(init?.headers || {}), enumerable: true },
ok: {
value: (init?.status || 200) >= 200 && (init?.status || 200) < 300,
enumerable: true,
},
redirected: { value: false, enumerable: true },
type: { value: 'basic', enumerable: true },
url: { value: '', enumerable: true },
bodyUsed: { value: false, writable: true },
})
}
static json(data, init = {}) {
return new MockResponse(JSON.stringify(data), {
...init,
headers: {
'Content-Type': 'application/json',
...(init?.headers || {}),
},
})
}
async json() {
return typeof this.body === 'string' ? JSON.parse(this.body) : this.body
}
async text() {
return typeof this.body === 'string' ? this.body : JSON.stringify(this.body)
}
async arrayBuffer() {
return new ArrayBuffer(0)
}
async blob() {
return new Blob([this.body || ''])
}
clone() {
return new MockResponse(this.body, {
status: this.status,
statusText: this.statusText,
headers: this.headers,
})
}
}
// Mock WebSocket
global.WebSocket = class MockWebSocket {
constructor() {
this.readyState = 0
}
send() {}
close() {}
addEventListener() {}
removeEventListener() {}
}
// Mock EventSource for SSE
global.EventSource = class MockEventSource {
constructor() {
this.readyState = 0
}
close() {}
addEventListener() {}
removeEventListener() {}
}
// Mock fetch
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve({}),
text: () => Promise.resolve(''),
headers: new Headers(),
status: 200,
statusText: 'OK',
})
)
// Mock Docker API
jest.mock('dockerode', () => {
return jest.fn().mockImplementation(() => ({
listContainers: jest.fn(() => Promise.resolve([])),
getContainer: jest.fn(() => ({
inspect: jest.fn(() => Promise.resolve({})),
stats: jest.fn(() => Promise.resolve({})),
logs: jest.fn(() => Promise.resolve('')),
start: jest.fn(() => Promise.resolve()),
stop: jest.fn(() => Promise.resolve()),
restart: jest.fn(() => Promise.resolve()),
})),
listImages: jest.fn(() => Promise.resolve([])),
listNetworks: jest.fn(() => Promise.resolve([])),
listVolumes: jest.fn(() => Promise.resolve({ Volumes: [] })),
}))
})
// Mock LokiJS
jest.mock('lokijs', () => {
const collections = new Map()
const createMockCollection = (name) => {
const collection = {
name,
data: [],
find: jest.fn(function (query = {}) {
if (!query || Object.keys(query).length === 0) {
return this.data
}
return this.data.filter((doc) => {
return Object.keys(query).every((key) => {
if (typeof query[key] === 'object' && query[key].$ne !== undefined) {
return doc[key] !== query[key].$ne
}
return doc[key] === query[key]
})
})
}),
findOne: jest.fn(function (query = {}) {
return (
this.data.find((doc) => Object.keys(query).every((key) => doc[key] === query[key])) ||
null
)
}),
insert: jest.fn(function (doc) {
const newDoc = { ...doc, $loki: this.data.length + 1 }
this.data.push(newDoc)
return newDoc
}),
update: jest.fn(function (doc) {
const index = this.data.findIndex((d) => d.$loki === doc.$loki)
if (index > -1) {
this.data[index] = doc
}
return doc
}),
remove: jest.fn(function (doc) {
const index = this.data.findIndex((d) => d.$loki === doc.$loki || d === doc)
if (index > -1) {
this.data.splice(index, 1)
}
}),
removeWhere: jest.fn(function (query) {
this.data = this.data.filter(
(doc) => !Object.keys(query).every((key) => doc[key] === query[key])
)
}),
clear: jest.fn(function () {
this.data = []
}),
count: jest.fn(function () {
return this.data.length
}),
// LokiJS unique-index O(1) lookup by field value
by: jest.fn(function (field, value) {
if (value === undefined) {
const col = this
return function (v) {
return col.by(field, v)
}
}
return this.data.find((doc) => doc[field] === value) || undefined
}),
// Add chain() method for LokiJS query chaining
chain: jest.fn(function () {
const resultset = {
_data: [...this.data],
find: jest.fn(function (query = {}) {
if (!query || Object.keys(query).length === 0) {
return this
}
this._data = this._data.filter((doc) =>
Object.keys(query).every((key) => {
if (typeof query[key] === 'object' && query[key].$ne !== undefined) {
return doc[key] !== query[key].$ne
}
return doc[key] === query[key]
})
)
return this
}),
where: jest.fn(function (filterFunc) {
this._data = this._data.filter(filterFunc)
return this
}),
simplesort: jest.fn(function (field, descending = false) {
this._data.sort((a, b) => {
const aVal = a[field]
const bVal = b[field]
if (aVal < bVal) return descending ? 1 : -1
if (aVal > bVal) return descending ? -1 : 1
return 0
})
return this
}),
offset: jest.fn(function (offsetVal) {
this._data = this._data.slice(offsetVal)
return this
}),
limit: jest.fn(function (limitVal) {
this._data = this._data.slice(0, limitVal)
return this
}),
data: jest.fn(function () {
return this._data
}),
}
return resultset
}),
}
return collection
}
return jest.fn().mockImplementation((filename, options = {}) => {
const collections = new Map()
const dbInstance = {
getCollection: jest.fn((name) => {
if (!collections.has(name)) {
collections.set(name, createMockCollection(name))
}
return collections.get(name)
}),
addCollection: jest.fn((name, collectionOptions) => {
const collection = createMockCollection(name)
collections.set(name, collection)
return collection
}),
loadDatabase: jest.fn((...args) => {
// LokiJS supports loadDatabase(callback) and loadDatabase(options, callback)
const callback = typeof args[0] === 'function' ? args[0] : args[1]
if (typeof callback === 'function') {
setTimeout(() => callback(), 0)
}
}),
saveDatabase: jest.fn((callback) => {
if (callback) {
setTimeout(() => callback(), 0)
}
}),
close: jest.fn(() => {}),
}
// If autoload is true, call autoloadCallback after a short delay
if (options.autoload && options.autoloadCallback) {
setTimeout(() => options.autoloadCallback(), 0)
}
return dbInstance
})
})
// Mock child_process exec
jest.mock('child_process', () => ({
exec: jest.fn((cmd, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
if (callback) {
callback(null, '', '')
}
return { stdout: '', stderr: '' }
}),
spawn: jest.fn(() => ({
stdout: { on: jest.fn(), pipe: jest.fn() },
stderr: { on: jest.fn(), pipe: jest.fn() },
on: jest.fn(),
kill: jest.fn(),
})),
}))
// Mock fs/promises
jest.mock('fs/promises', () => ({
readFile: jest.fn(() => Promise.resolve('')),
writeFile: jest.fn(() => Promise.resolve()),
unlink: jest.fn(() => Promise.resolve()),
mkdir: jest.fn(() => Promise.resolve()),
readdir: jest.fn(() => Promise.resolve([])),
stat: jest.fn(() => Promise.resolve({ isDirectory: () => false })),
access: jest.fn(() => Promise.resolve()),
}))
// Mock fs
jest.mock('fs', () => ({
...jest.requireActual('fs'),
existsSync: jest.fn(() => true),
readFileSync: jest.fn(() => ''),
writeFileSync: jest.fn(() => {}),
createReadStream: jest.fn(() => ({
on: jest.fn(),
pipe: jest.fn(),
})),
createWriteStream: jest.fn(() => ({
on: jest.fn(),
write: jest.fn(),
end: jest.fn(),
})),
}))
// Mock next/headers
jest.mock('next/headers', () => ({
cookies: jest.fn(() => ({
get: jest.fn(() => ({ value: 'mock-token' })),
set: jest.fn(),
delete: jest.fn(),
})),
headers: jest.fn(() => ({
get: jest.fn(() => null),
})),
}))
// Mock next/server (NextRequest, NextResponse)
jest.mock('next/server', () => {
const { Request, Response, Headers } = global
class MockNextRequest extends Request {
constructor(input, init = {}) {
// Call parent constructor with URL and init
super(input, init)
// Add NextRequest-specific properties
// input may be a string, a URL object (has .href), or a Request (has .url)
const urlStr =
typeof input === 'string' ? input : input instanceof URL ? input.href : input.url
const url = new URL(urlStr)
Object.defineProperties(this, {
nextUrl: {
value: {
pathname: url.pathname,
search: url.search,
searchParams: new URLSearchParams(url.search),
href: url.href,
origin: url.origin,
protocol: url.protocol,
host: url.host,
hostname: url.hostname,
port: url.port,
hash: url.hash,
},
enumerable: true,
},
cookies: {
value: {
get: jest.fn((name) => {
const cookieHeader = this.headers.get('cookie') || ''
const cookies = Object.fromEntries(
cookieHeader
.split(';')
.map((c) => {
const [key, ...values] = c.trim().split('=')
return [key, values.join('=')]
})
.filter(([key]) => key)
)
return cookies[name] ? { name, value: cookies[name] } : undefined
}),
getAll: jest.fn(() => []),
set: jest.fn(),
delete: jest.fn(),
has: jest.fn(),
clear: jest.fn(),
},
enumerable: true,
},
geo: { value: {}, enumerable: true },
ip: { value: '127.0.0.1', enumerable: true },
})
}
}
class MockNextResponse extends Response {
constructor(body, init = {}) {
super(body, init)
// Add NextResponse-specific cookies API
const cookieJar = []
Object.defineProperty(this, 'cookies', {
value: {
set: jest.fn((name, value, options = {}) => {
const cookieString = `${name}=${value}`
cookieJar.push({ name, value, options })
const existingCookies = this.headers.get('set-cookie') || ''
const newCookies = existingCookies
? `${existingCookies}, ${cookieString}`
: cookieString
this.headers.set('set-cookie', newCookies)
}),
get: jest.fn((name) => {
const cookie = cookieJar.find((c) => c.name === name)
return cookie ? { name: cookie.name, value: cookie.value } : undefined
}),
getAll: jest.fn(() => cookieJar),
delete: jest.fn((name) => {
const index = cookieJar.findIndex((c) => c.name === name)
if (index > -1) cookieJar.splice(index, 1)
}),
has: jest.fn((name) => cookieJar.some((c) => c.name === name)),
clear: jest.fn(() => {
cookieJar.length = 0
}),
},
enumerable: true,
})
}
static json(data, init = {}) {
const response = new MockNextResponse(JSON.stringify(data), {
...init,
headers: {
'Content-Type': 'application/json',
...(init?.headers || {}),
},
})
return response
}
static redirect(url, init) {
return new MockNextResponse(null, {
...init,
status: init?.status || 307,
headers: {
Location: url,
...(init?.headers || {}),
},
})
}
static rewrite(url, init) {
return new MockNextResponse(null, {
...init,
headers: {
'x-middleware-rewrite': url,
...(init?.headers || {}),
},
})
}
static next(init) {
return new MockNextResponse(null, {
...init,
headers: {
'x-middleware-next': '1',
...(init?.headers || {}),
},
})
}
}
return {
NextRequest: MockNextRequest,
NextResponse: MockNextResponse,
}
})
// Mock next/navigation
jest.mock('next/navigation', () => ({
useRouter: jest.fn(() => ({
push: jest.fn(),
replace: jest.fn(),
back: jest.fn(),
forward: jest.fn(),
refresh: jest.fn(),
prefetch: jest.fn(),
})),
usePathname: jest.fn(() => '/'),
useSearchParams: jest.fn(() => new URLSearchParams()),
redirect: jest.fn(),
notFound: jest.fn(),
}))
// Mock next/cache
jest.mock('next/cache', () => ({
revalidatePath: jest.fn(),
revalidateTag: jest.fn(),
unstable_cache: jest.fn((fn) => fn),
}))
// Suppress console errors in tests unless needed
const originalError = console.error
beforeAll(() => {
console.error = jest.fn()
})
afterAll(() => {
console.error = originalError
})