-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
559 lines (505 loc) · 18.1 KB
/
Copy pathvitest.setup.ts
File metadata and controls
559 lines (505 loc) · 18.1 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
/**
* VITEST SETUP - COMPREHENSIVE TEST ENVIRONMENT
*
* Configures the Vitest environment with the mocks, polyfills, and global
* configuration OrangeCat tests rely on. Ported from jest.setup.ts when the
* runner moved from jest 30 + ts-jest (CJS) to Vitest (ESM-native).
*/
/* eslint-disable no-console */
import '@testing-library/jest-dom/vitest';
import { vi, beforeEach, afterAll } from 'vitest';
import { TextEncoder as NodeTextEncoder, TextDecoder as NodeTextDecoder } from 'node:util';
// jsdom ships no TextEncoder/TextDecoder; server-side modules (e.g. the Solon
// Bitcoin-message crypto) use them at import time. Node's are spec-compliant.
if (typeof globalThis.TextEncoder === 'undefined') {
globalThis.TextEncoder = NodeTextEncoder as typeof globalThis.TextEncoder;
globalThis.TextDecoder = NodeTextDecoder as typeof globalThis.TextDecoder;
}
// -----------------------------------------------------------------------------
// Global Test Environment Configuration
// -----------------------------------------------------------------------------
// Provide sane default environment variables so modules that read them do not
// throw during import when individual test files forget to set them explicitly.
process.env.NEXT_PUBLIC_SITE_URL = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000';
process.env.NEXT_PUBLIC_SITE_NAME = process.env.NEXT_PUBLIC_SITE_NAME || 'OrangeCat';
process.env = Object.assign({ NODE_ENV: 'test' }, process.env);
// JSDOM already provides localStorage / sessionStorage but not with vi.fn()
// Create spy-able versions so tests can safely mock implementations.
function createStorageMock() {
const storage: Record<string, string> = {};
return {
getItem: vi.fn((key: string) => (key in storage ? storage[key] : null)),
setItem: vi.fn((key: string, value: string) => {
storage[key] = value;
}),
removeItem: vi.fn((key: string) => {
delete storage[key];
}),
clear: vi.fn(() => {
Object.keys(storage).forEach(k => delete storage[k]);
}),
key: vi.fn((index: number) => Object.keys(storage)[index] ?? null),
get length() {
return Object.keys(storage).length;
},
};
}
// Suites may opt into the node environment (@vitest-environment node) for pure
// server-side code — every browser-global touch below must be conditional.
const IS_JSDOM = typeof window !== 'undefined';
// Replace window.localStorage / sessionStorage with our spy-able mocks.
const localStorageMockInstance = createStorageMock();
const sessionStorageMockInstance = createStorageMock();
if (IS_JSDOM) {
Object.defineProperty(window, 'localStorage', {
value: localStorageMockInstance,
writable: true,
});
Object.defineProperty(window, 'sessionStorage', {
value: sessionStorageMockInstance,
writable: true,
});
}
// Prevent React act warnings from polluting test output in older tests.
// (These are warning-level logs; suppress them globally.)
vi.spyOn(console, 'error').mockImplementation((...args) => {
if (typeof args[0] === 'string' && args[0].includes('not wrapped in act')) {
return;
}
return (console.error as any).original?.(...args);
});
// Reset the module cache before every test so modules that run side-effects on
// import (e.g. Supabase client which calls createBrowserClient and logger)
// execute fresh.
beforeEach(() => {
vi.resetModules();
});
// =====================================================================
// 🌍 ENVIRONMENT VARIABLES SETUP
// =====================================================================
// Set up test environment variables
Object.defineProperty(process.env, 'NODE_ENV', { value: 'test', writable: true });
Object.defineProperty(process.env, 'NEXT_PUBLIC_SUPABASE_URL', {
value: 'https://test.supabase.co',
writable: true,
});
Object.defineProperty(process.env, 'NEXT_PUBLIC_SUPABASE_ANON_KEY', {
value: 'test-anon-key',
writable: true,
});
Object.defineProperty(process.env, 'SUPABASE_SERVICE_ROLE_KEY', {
value: 'test-service-role-key',
writable: true,
});
// =====================================================================
// 🔧 GLOBAL MOCKS & POLYFILLS
// =====================================================================
// Mock fetch globally
global.fetch = vi.fn();
// localStorage / sessionStorage are already replaced above with spy-able,
// store-backed mocks (createStorageMock) — these are just handles to them.
// One mock, one source of truth: don't define a second stub here.
const localStorageMock = localStorageMockInstance;
const sessionStorageMock = sessionStorageMockInstance;
// Mock window.location
if (IS_JSDOM) {
delete (window as any).location;
window.location = {
href: 'http://localhost:3000',
origin: 'http://localhost:3000',
protocol: 'http:',
host: 'localhost:3000',
hostname: 'localhost',
port: '3000',
pathname: '/',
search: '',
hash: '',
assign: vi.fn(),
replace: vi.fn(),
reload: vi.fn(),
} as any;
}
// Mock console methods to reduce noise in tests
const originalConsole = { ...console };
global.console = {
...console,
log: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
info: vi.fn(),
debug: vi.fn(),
};
// =====================================================================
// 🎨 LUCIDE ICONS MOCK
// =====================================================================
// Every icon renders as a real `<svg>` carrying its own name, so tests can
// still assert that the right icon appeared — a blanket `() => null` would
// quietly make icon-presence assertions pass on nothing. This used to be a
// hand-written allowlist of ~90 icon names, which meant every icon NOT on the
// list resolved to `undefined` and blew up as "Element type is invalid" — an
// error that reads like a broken component and has twice been debugged as one.
// It lives here as a vi.mock factory (not a resolve.alias to a CJS file):
// the mock is a Proxy so ANY icon name resolves, and CJS interop of an aliased
// Proxy module would surface no named exports (a Proxy has no own keys).
vi.mock('lucide-react', async () => {
const React = await import('react');
const iconComponent = (name: string) => {
const Icon = React.forwardRef<SVGSVGElement, Record<string, any>>((props, ref) =>
React.createElement('svg', {
ref,
'data-testid': props['data-testid'] ?? `icon-${name}`,
'data-icon': name,
'aria-hidden': props['aria-hidden'],
className: props.className,
})
);
Icon.displayName = name;
return Icon;
};
const cache = new Map<string, unknown>();
return new Proxy(
{},
{
// Vitest verifies an export exists with an `in` check before reading it.
has(_target, prop) {
return typeof prop === 'string';
},
get(_target, prop) {
if (prop === '__esModule') {
return true;
}
if (typeof prop !== 'string' || prop === 'default' || prop === 'then') {
return undefined;
}
// `createLucideIcon` and friends are factories, not icons; anything
// else accessed off this module in a component is an icon.
if (!cache.has(prop)) {
cache.set(prop, iconComponent(prop));
}
return cache.get(prop);
},
}
);
});
// =====================================================================
// 🔐 AUTH STORE MOCKS
// =====================================================================
vi.mock('@/stores/auth', () => ({
useAuthStore: vi.fn(() => ({
user: null,
session: null,
profile: null,
isLoading: false,
error: null,
signIn: vi.fn(),
signUp: vi.fn(),
signOut: vi.fn(),
updateProfile: vi.fn(),
clearError: vi.fn(),
setUser: vi.fn(),
setSession: vi.fn(),
setProfile: vi.fn(),
})),
}));
// =====================================================================
// 🎨 UI COMPONENT MOCKS
// =====================================================================
// Mock Sonner toast
vi.mock('sonner', () => ({
toast: {
success: vi.fn(),
error: vi.fn(),
info: vi.fn(),
warning: vi.fn(),
loading: vi.fn(),
dismiss: vi.fn(),
},
}));
// Lucide icons are mocked in __mocks__/lucide-react.js (wired through
// resolve.alias in vitest.config.ts). This used to be a hand-written allowlist
// of ~90 icon names, which meant every icon NOT on the list resolved to
// `undefined` and blew up as "Element type is invalid" — an error that reads
// like a broken component and has twice been debugged as one. A component
// should never need a test-config edit just to use a different icon.
// =====================================================================
// 🧪 TEST UTILITIES
// =====================================================================
// Global test utilities
global.testUtils = {
// Reset all mocks
resetMocks: () => {
vi.clearAllMocks();
localStorageMock.getItem.mockClear();
localStorageMock.setItem.mockClear();
localStorageMock.removeItem.mockClear();
localStorageMock.clear.mockClear();
sessionStorageMock.getItem.mockClear();
sessionStorageMock.setItem.mockClear();
sessionStorageMock.removeItem.mockClear();
sessionStorageMock.clear.mockClear();
},
// Mock successful API responses
mockSuccessResponse: (data: any) => {
(global.fetch as ReturnType<typeof vi.fn>).mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => data,
text: async () => JSON.stringify(data),
});
},
// Mock error API responses
mockErrorResponse: (status: number, message: string) => {
(global.fetch as ReturnType<typeof vi.fn>).mockResolvedValueOnce({
ok: false,
status,
json: async () => ({ error: message }),
text: async () => message,
});
},
// Mock network error
mockNetworkError: (message: string = 'Network error') => {
(global.fetch as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error(message));
},
};
// =====================================================================
// 🔄 SETUP & TEARDOWN
// =====================================================================
// Before each test
beforeEach(() => {
// Reset all mocks
vi.clearAllMocks();
// Empty the storage backing stores, then reset call history, so every test
// starts with clean storage and clean spies.
localStorageMock.clear();
sessionStorageMock.clear();
if (localStorageMock.getItem?.mockClear) {
localStorageMock.getItem.mockClear();
localStorageMock.setItem.mockClear();
localStorageMock.removeItem.mockClear();
localStorageMock.clear.mockClear();
}
if (sessionStorageMock.getItem?.mockClear) {
sessionStorageMock.getItem.mockClear();
sessionStorageMock.setItem.mockClear();
sessionStorageMock.removeItem.mockClear();
sessionStorageMock.clear.mockClear();
}
// Reset fetch mock
if (global.fetch && (global.fetch as any).mockClear) {
(global.fetch as any).mockClear();
}
// Reset console mocks
if ((console.log as any).mockClear) {
(console.log as any).mockClear();
(console.warn as any).mockClear();
(console.error as any).mockClear();
(console.info as any).mockClear();
(console.debug as any).mockClear();
}
});
// After all tests
afterAll(() => {
// Restore original console
global.console = originalConsole;
});
// =====================================================================
// 🎯 TYPE DECLARATIONS
// =====================================================================
declare global {
var testUtils: {
resetMocks: () => void;
mockSuccessResponse: (data: any) => void;
mockErrorResponse: (status: number, message: string) => void;
mockNetworkError: (message?: string) => void;
};
}
// =====================================================================
// 🗄️ SUPABASE-JS GLOBAL MOCK (createClient)
// =====================================================================
vi.mock('@supabase/supabase-js', () => {
// Simple in-memory store per table for rudimentary insert/select/update/delete
const inMemoryDB: Record<string, any[]> = {};
function tableStore(name: string) {
if (!inMemoryDB[name]) {
inMemoryDB[name] = [];
}
return inMemoryDB[name];
}
const buildQuery = (table: string, dataset: any[]): any => {
const builder = {
_table: table,
_store: tableStore(table),
_results: dataset,
select(this: any, _columns = '*', opts?: any) {
if (opts && opts.count) {
return Promise.resolve({ data: this._results, count: this._results.length, error: null });
}
return buildQuery(this._table, this._results);
},
eq(this: any, column: string, value: any) {
const filtered = this._results.filter((row: any) => row[column] === value);
return buildQuery(this._table, filtered);
},
order(this: any, column: string, { ascending = true } = {}) {
const sorted = [...this._results].sort((a, b) => {
if (a[column] === b[column]) {
return 0;
}
return ascending ? (a[column] > b[column] ? 1 : -1) : a[column] < b[column] ? 1 : -1;
});
return buildQuery(this._table, sorted);
},
range(this: any, from: number, to: number) {
const ranged = this._results.slice(from, to + 1);
return buildQuery(this._table, ranged);
},
limit(this: any, count: number) {
const limited = this._results.slice(0, count);
return Promise.resolve({ data: limited, error: null });
},
single(this: any) {
if (this._results.length === 0) {
return Promise.resolve({ data: null, error: null });
}
return Promise.resolve({ data: this._results[0], error: null });
},
maybeSingle(this: any) {
if (this._results.length === 0) {
return Promise.resolve({ data: null, error: null });
}
return Promise.resolve({ data: this._results[0], error: null });
},
update(this: any, values: any) {
this._results.forEach((row: any) => Object.assign(row, values));
return buildQuery(this._table, this._results);
},
upsert(this: any, obj: any) {
const targetArr = this._store;
const items = Array.isArray(obj) ? obj : [obj];
items.forEach(item => {
const existingIdx = targetArr.findIndex((r: any) => r.id === item.id);
if (existingIdx !== -1) {
targetArr[existingIdx] = { ...targetArr[existingIdx], ...item };
} else {
targetArr.push(item);
}
});
return buildQuery(this._table, items);
},
delete(this: any) {
this._results.forEach((row: any) => {
const idx = this._store.indexOf(row);
if (idx !== -1) {
this._store.splice(idx, 1);
}
});
return Promise.resolve({ data: null, error: null });
},
count(this: any) {
return Promise.resolve({ count: this._results.length, error: null });
},
};
return builder;
};
function from(tableName: string) {
const storeRef = tableStore(tableName);
const baseBuilder = buildQuery(tableName, storeRef);
return {
...baseBuilder,
insert(rows: any) {
const arr = tableStore(tableName);
const inserted = Array.isArray(rows) ? rows : [rows];
arr.push(...inserted);
return buildQuery(tableName, inserted);
},
};
}
// Seed base data for tests
const seedProfiles = () => {
const profiles = tableStore('profiles');
if (profiles.length === 0) {
profiles.push(
{
id: 'test-user',
username: 'orangecat',
display_name: 'Orange Cat',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
},
{
id: 'search-1',
username: 'searchtest1',
display_name: 'Search User 1',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
},
{
id: 'search-2',
username: 'searchtest2',
display_name: 'Search User 2',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
},
{
id: 'user-123',
username: 'existinguser',
display_name: 'Existing User',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
}
);
}
};
seedProfiles();
const mockClient = {
from,
auth: {
signInWithPassword: vi.fn(({ email }) =>
Promise.resolve({ data: { user: { id: 'test-user', email } }, error: null })
),
getUser: vi.fn(() =>
Promise.resolve({
data: { user: { id: 'test-user', email: 'test@example.com' } },
error: null,
})
),
getSession: vi.fn(() =>
Promise.resolve({ data: { session: { access_token: 'token' } }, error: null })
),
},
rpc: vi.fn(() => Promise.resolve({ data: null, error: null })),
};
return {
__esModule: true,
createClient: vi.fn(() => mockClient),
};
});
// Ensure Storage.prototype functions are vi.fn for spying in tests
if (typeof Storage !== 'undefined') {
Object.defineProperty(Storage.prototype, 'setItem', { value: vi.fn(), writable: true });
Object.defineProperty(Storage.prototype, 'getItem', { value: vi.fn(), writable: true });
}
// =====================================================================
// 🧪 PROFILE MAPPER MOCKS
// =====================================================================
// Additional mapper mock for Profile tests
vi.mock('@/services/profile/mapper', () => ({
ProfileMapper: {
mapDatabaseToProfile: (data: any) => data,
mapProfileToDatabase: (data: any) => data,
},
}));
// =====================================================================
// 🧪 PROFILE READER MOCKS
// =====================================================================
vi.mock('@/services/profile/reader', () => {
return {
ProfileReader: {
getProfile: vi.fn(() => Promise.resolve(null)),
getProfiles: vi.fn(() => Promise.resolve([])),
searchProfiles: vi.fn(() => Promise.resolve([])),
getAllProfiles: vi.fn(() => Promise.resolve([])),
incrementProfileViews: vi.fn(),
},
};
});