-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
136 lines (124 loc) · 3.33 KB
/
jest.setup.js
File metadata and controls
136 lines (124 loc) · 3.33 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
// Learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom'
// Mock ResizeObserver
class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
}
window.ResizeObserver = ResizeObserver
// Mock Headers class with factory
global.Headers = class Headers {
constructor(init) {
this._init = init
}
get(name) {
// Mock implementation - returns undefined or the header value
return this._init?.[name.toLowerCase()] || undefined
}
has(name) {
return this._init?.hasOwnProperty(name.toLowerCase()) || false
}
[Symbol.iterator]() {
return this
}
next() {
return { done: true, value: undefined }
}
}
// Mock Next.js global Request
global.Request = class Request {}
// Mock Node.js built-in modules before they're used
global.fetch = global.fetch || (() => {
// Store the real implementation
const realFetch = global.fetch || (() => null)
return async (url, options = {}) => {
try {
return await realFetch(url, options)
} catch (e) {
// If real fetch throws, return a mock response
if (options.method !== 'GET') {
throw e
}
return Promise.resolve({
ok: true,
status: 200,
headers: new Headers(),
json: async () => (options.body || {}),
text: async () => options.body || ''
})
}
}
})()
// Mock FormData
global.FormData = class FormData {
constructor() {
this.data = {}
}
append(name, value) {
this.data[name] = value
}
get(name) {
return this.data[name]
}
getAll(name) {
return this.data[name] || []
}
}
// Mock Next.js Response - this must be done before Next.js imports it
class MockResponse {
constructor(body, init = {}) {
this._body = body
this._text = typeof body === 'string' ? body : JSON.stringify(body)
this.status = init.status || 200
this.headers = new Headers(init.headers || {})
// Parse JSON body if it's a valid JSON string
this._json = (() => {
if (typeof body === 'string') {
try {
const parsed = JSON.parse(body)
return parsed
} catch {
return null
}
}
return body || {}
})()
this.getSetCookie = () => []
this.clone = () => this.clone()
this.text = async () => this._text
this.json = async () => {
const contentType = this.headers.get('Content-Type') || ''
// Return parsed JSON if available, otherwise return text
const result = this._json !== null ? this._json : this._text
console.log('MockResponse.json called', { _json: this._json, _text: this._text, result })
return result
}
}
clone() {
const cloned = new MockResponse(this._body, { status: this.status, headers: this.headers })
cloned._text = this._text
cloned._json = this._json
cloned.status = this.status
return cloned
}
}
// Override the Response class globally
global.Response = MockResponse
// Mock NextResponse
global.NextResponse = class NextResponse extends MockResponse {
static redirect(url, status = 308) {
return new MockResponse('', { status })
}
}
// Mock Next.js global fetch with minimal implementation
global.fetch = async (url, init) => {
// Minimal mock for fetch
return {
ok: true,
status: 200,
json: async () => ({}),
text: async () => '{}',
headers: new Headers()
}
}