-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
169 lines (149 loc) · 4.2 KB
/
Copy pathvitest.setup.ts
File metadata and controls
169 lines (149 loc) · 4.2 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
import { vi } from 'vitest';
// Mock environment variables
process.env.NODE_ENV = 'test';
process.env.JWT_SECRET = 'test-jwt-secret';
process.env.DATABASE_URL = 'postgresql://test:test@localhost:5432/antbase_test';
process.env.REDIS_URL = 'redis://localhost:6379/1';
process.env.OPENAI_API_KEY = 'test-openai-key';
// Mock console methods in test environment
global.console = {
...console,
// Keep these for test output
log: console.log,
warn: console.warn,
error: console.error,
// Mock these to reduce noise
debug: vi.fn(),
info: vi.fn(),
};
// Mock fetch globally
global.fetch = vi.fn();
// Mock WebSocket
global.WebSocket = vi.fn(() => ({
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
send: vi.fn(),
close: vi.fn(),
readyState: 1,
})) as any;
// Mock localStorage
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem: (key: string) => store[key] || null,
setItem: (key: string, value: string) => {
store[key] = value;
},
removeItem: (key: string) => {
delete store[key];
},
clear: () => {
store = {};
},
length: Object.keys(store).length,
key: (index: number) => Object.keys(store)[index] || null,
};
})();
Object.defineProperty(global, 'localStorage', {
value: localStorageMock,
});
// Mock sessionStorage
Object.defineProperty(global, 'sessionStorage', {
value: localStorageMock,
});
// Mock URL
global.URL = {
createObjectURL: vi.fn(() => 'mocked-url'),
revokeObjectURL: vi.fn(),
} as any;
// Mock crypto
Object.defineProperty(global, 'crypto', {
value: {
randomUUID: vi.fn(() => 'mocked-uuid'),
subtle: {
digest: vi.fn(),
encrypt: vi.fn(),
decrypt: vi.fn(),
},
},
});
// Mock ResizeObserver
global.ResizeObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));
// Mock IntersectionObserver
global.IntersectionObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
root: null,
rootMargin: '',
thresholds: [],
}));
// Mock File and FileReader
global.File = vi.fn().mockImplementation((fileBits, fileName, options) => ({
name: fileName,
size: fileBits.reduce((acc: number, bit: any) => acc + bit.length, 0),
type: options?.type || '',
lastModified: Date.now(),
})) as any;
global.FileReader = vi.fn().mockImplementation(() => ({
readAsText: vi.fn(function(this: any) {
this.onload?.({ target: { result: 'file content' } });
}),
readAsDataURL: vi.fn(function(this: any) {
this.onload?.({ target: { result: 'data:text/plain;base64,ZmlsZSBjb250ZW50' } });
}),
result: null,
error: null,
onload: null,
onerror: null,
})) as any;
// Mock Blob
global.Blob = vi.fn().mockImplementation((content, options) => ({
size: content?.reduce?.((acc: number, item: any) => acc + item.length, 0) || 0,
type: options?.type || '',
slice: vi.fn(),
stream: vi.fn(),
text: vi.fn().mockResolvedValue('blob content'),
})) as any;
// Mock FormData
global.FormData = vi.fn().mockImplementation(() => {
const data = new Map();
return {
append: vi.fn((key, value) => data.set(key, value)),
delete: vi.fn((key) => data.delete(key)),
get: vi.fn((key) => data.get(key)),
has: vi.fn((key) => data.has(key)),
set: vi.fn((key, value) => data.set(key, value)),
entries: vi.fn(() => data.entries()),
keys: vi.fn(() => data.keys()),
values: vi.fn(() => data.values()),
};
}) as any;
// Mock URLSearchParams
global.URLSearchParams = vi.fn().mockImplementation(() => {
const params = new Map();
return {
append: vi.fn((key, value) => params.set(key, value)),
delete: vi.fn((key) => params.delete(key)),
get: vi.fn((key) => params.get(key)),
has: vi.fn((key) => params.has(key)),
set: vi.fn((key, value) => params.set(key, value)),
toString: vi.fn(() => Array.from(params.entries()).map(([k, v]) => `${k}=${v}`).join('&')),
};
}) as any;
// Setup test database connection
beforeAll(() => {
// This would be implemented based on your database setup
});
afterAll(() => {
// Cleanup test database
});
// Reset all mocks between tests
afterEach(() => {
vi.clearAllMocks();
localStorageMock.clear();
});