Repository navigation
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
50 lines (44 loc) · 1.43 KB
/
Copy pathvitest.setup.ts
File metadata and controls
50 lines (44 loc) · 1.43 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
import '@testing-library/jest-dom';
import { vi } from 'vitest';
import { Agent, setGlobalDispatcher } from 'undici';
// Disable keep-alive for tests to prevent ECONNRESET errors
// Use pipelining=0 instead of keepAlive=false for this version of undici
setGlobalDispatcher(new Agent({ pipelining: 0 }));
// Mock fetch globally for component tests, but not for server integration tests
if (!process.env.VITEST_SERVER_TEST) {
global.fetch = vi.fn();
}
// Mock console.error to suppress error logs during tests
global.console.error = vi.fn();
// Mock window.getComputedStyle
Object.defineProperty(window, 'getComputedStyle', {
value: () => ({
getPropertyValue: () => '',
}),
});
// Global test utilities
export const mockFetch = (response: any, revisionsResponse?: any) => {
(global.fetch as any).mockImplementation((url: string) => {
// Handle /api/revisions endpoint
if (url.includes('/api/revisions')) {
return Promise.resolve({
ok: revisionsResponse !== null,
json: async () =>
revisionsResponse ?? {
specialOptions: [],
branches: [],
commits: [],
},
});
}
// Default: /api/diff and others
return Promise.resolve({
ok: true,
json: async () => response,
blob: async () => ({ size: 1024 }),
});
});
};
export const mockFetchError = (error: string) => {
(global.fetch as any).mockRejectedValue(new Error(error));
};