-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.ts
More file actions
127 lines (109 loc) · 3.59 KB
/
Copy pathjest.setup.ts
File metadata and controls
127 lines (109 loc) · 3.59 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
import '@testing-library/jest-dom/matchers';
import '@testing-library/jest-dom';
// Web API polyfills for Next.js API routes
import 'whatwg-fetch'; // Provides proper fetch, Request, Response, Headers
// IndexedDB polyfill for testing Dexie
import 'fake-indexeddb/auto';
// Mock window object for client-side checks in tests
// Note: jsdom provides localStorage, but we need to ensure it's available in our window mock
Object.defineProperty(global, 'window', {
value: {
location: { href: 'http://localhost' },
document: {},
navigator: { userAgent: 'test' },
localStorage: global.localStorage // Use jsdom's localStorage
},
writable: true
});
// Polyfill for structuredClone (used by fake-indexeddb)
if (typeof global.structuredClone === 'undefined') {
global.structuredClone = (value: any) => {
return JSON.parse(JSON.stringify(value));
};
}
// Ensure all Web APIs are available
if (typeof global.Request === 'undefined') {
global.Request = require('whatwg-fetch').Request;
}
if (typeof global.Response === 'undefined') {
global.Response = require('whatwg-fetch').Response;
}
if (typeof global.Headers === 'undefined') {
global.Headers = require('whatwg-fetch').Headers;
}
// Mock NextResponse to work properly in Jest environment
jest.mock('next/server', () => ({
NextResponse: {
json: jest.fn((body: any, init?: any) => {
const jsonString = JSON.stringify(body);
const mockResponse = new Response(jsonString, {
...init,
headers: {
'Content-Type': 'application/json',
...init?.headers
}
});
// Add json method to the response instance that properly parses JSON
// This simulates the actual browser/Node.js Response.json() behavior
(mockResponse as any).json = jest.fn(() => {
return Promise.resolve(JSON.parse(jsonString));
});
return mockResponse;
})
}
}));
// Mock global performance API for monitoring tests
global.performance = {
now: jest.fn(() => Date.now()),
mark: jest.fn(),
measure: jest.fn(),
clearMarks: jest.fn(),
clearMeasures: jest.fn(),
getEntriesByName: jest.fn(),
getEntriesByType: jest.fn(),
getEntries: jest.fn(),
} as any;
// Mock fetch for webhook tests
global.fetch = jest.fn();
// Mock crypto.randomUUID for consistent test IDs
global.crypto = {
randomUUID: jest.fn(() => 'test-uuid-12345'),
} as any;
// Polyfill setImmediate for jsdom environment (used in async tests)
if (typeof global.setImmediate === 'undefined') {
global.setImmediate = jest.fn((fn: Function) => {
setTimeout(fn, 0);
return {} as any;
}) as any;
}
// Mock Supabase modules to avoid ES module issues
jest.mock('@supabase/supabase-js', () => ({
createClient: jest.fn(() => ({
from: jest.fn(),
auth: { getSession: jest.fn() },
storage: {}
}))
}));
jest.mock('@supabase/ssr', () => ({
createBrowserClient: jest.fn(),
createServerClient: jest.fn()
}));
// Suppress console logs in tests unless explicitly testing them
// const originalConsole = { ...console };
// beforeEach(() => {
// jest.spyOn(console, 'log').mockImplementation(() => {});
// jest.spyOn(console, 'warn').mockImplementation(() => {});
// jest.spyOn(console, 'error').mockImplementation(() => {});
// });
// afterEach(() => {
// // Restore console if not explicitly mocked in test
// if (!jest.isMockFunction(console.log)) {
// console.log = originalConsole.log;
// }
// if (!jest.isMockFunction(console.warn)) {
// console.warn = originalConsole.warn;
// }
// if (!jest.isMockFunction(console.error)) {
// console.error = originalConsole.error;
// }
// });