-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
172 lines (148 loc) · 4.04 KB
/
Copy pathjest.setup.js
File metadata and controls
172 lines (148 loc) · 4.04 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
/**
* Jest Setup File
*
* Runs before each test file.
* Used for:
* - Importing jest-dom matchers
* - Setting up global mocks
* - Configuring test environment
*/
// Import jest-dom for additional matchers
import '@testing-library/jest-dom';
// Mock environment variables for tests
process.env.NEXT_PUBLIC_MAPBOX_TOKEN = 'pk.test.mock_mapbox_token';
process.env.NEXT_PUBLIC_SUPABASE_URL = 'https://test.supabase.co';
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY = 'test_anon_key';
process.env.SUPABASE_URL = 'https://test.supabase.co';
process.env.SUPABASE_ANON_KEY = 'test_anon_key';
process.env.OPENAI_API_KEY = 'sk-test-mock-openai-key';
process.env.ANTHROPIC_API_KEY = 'test-mock-anthropic-key';
process.env.NEXT_PUBLIC_BASE_URL = 'http://localhost:3000';
// Mock fetch globally for API tests
global.fetch = jest.fn();
// Mock console methods to reduce noise in tests (but keep errors)
const originalError = console.error;
const originalWarn = console.warn;
beforeAll(() => {
console.error = jest.fn((...args) => {
// Still log actual errors from tests
if (
typeof args[0] === 'string' &&
(args[0].includes('Warning:') || args[0].includes('Error:'))
) {
originalError(...args);
}
});
console.warn = jest.fn((...args) => {
// Filter out known warnings
if (
typeof args[0] === 'string' &&
!args[0].includes('act()')
) {
originalWarn(...args);
}
});
});
afterAll(() => {
console.error = originalError;
console.warn = originalWarn;
});
// Increase timeout for slower tests (API calls, etc.)
jest.setTimeout(10000);
// Mock IntersectionObserver (used by some components)
global.IntersectionObserver = class IntersectionObserver {
constructor() {}
disconnect() {}
observe() {}
takeRecords() {
return [];
}
unobserve() {}
};
// Mock ResizeObserver (used by Chart.js and other libraries)
global.ResizeObserver = class ResizeObserver {
constructor() {}
disconnect() {}
observe() {}
unobserve() {}
};
// Mock matchMedia (used for responsive design)
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
// Mock scrollTo (used for smooth scrolling)
window.scrollTo = jest.fn();
// Mock Next.js Request and Response for API route tests
global.Request = class Request {
constructor(url, init = {}) {
// Use Object.defineProperty for readonly properties to avoid conflicts with NextRequest
Object.defineProperty(this, 'url', {
value: url,
writable: false,
enumerable: true,
configurable: true,
});
Object.defineProperty(this, 'method', {
value: init.method || 'GET',
writable: false,
enumerable: true,
configurable: true,
});
this.headers = new Map();
// Handle headers from init
if (init.headers) {
Object.entries(init.headers).forEach(([key, value]) => {
this.headers.set(key.toLowerCase(), value);
});
}
// Handle body
this._bodyInit = init.body;
}
get(name) {
return this.headers.get(name.toLowerCase());
}
async json() {
if (this._bodyInit) {
return JSON.parse(this._bodyInit);
}
return {};
}
async text() {
return this._bodyInit || '';
}
};
global.Response = class Response {
constructor(body, init = {}) {
this.body = body;
this.status = init.status || 200;
this.statusText = init.statusText || 'OK';
this.headers = new Map();
if (init.headers) {
Object.entries(init.headers).forEach(([key, value]) => {
this.headers.set(key.toLowerCase(), value);
});
}
}
async json() {
if (typeof this.body === 'string') {
return JSON.parse(this.body);
}
return this.body;
}
async text() {
if (typeof this.body === 'string') {
return this.body;
}
return JSON.stringify(this.body);
}
};