-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
84 lines (73 loc) · 1.91 KB
/
vitest.setup.ts
File metadata and controls
84 lines (73 loc) · 1.91 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
import { vi } from 'vitest';
import '@testing-library/jest-dom/vitest';
import 'jest-axe/extend-expect';
// Type definitions for mocks
interface ImageProps {
src: string;
alt: string;
[key: string]: unknown;
}
// Mock Astro components for testing
vi.mock('astro:assets', () => ({
Image: ({ src, alt }: ImageProps) => {
return `<img src="${src}" alt="${alt}" />`;
}
}));
// Mock client directives (they don't run in test environment)
vi.mock('astro:client', () => ({}));
// Mock GA script loading to prevent network errors in tests
vi.mock('@/lib/utils', async () => {
const actual = await vi.importActual<typeof import('@/lib/utils')>('@/lib/utils');
return {
...actual,
loadGAScript: vi.fn(), // Prevent GA script fetch during tests
updateGAConsent: vi.fn() // Mock consent updates
};
});
// Mock global fetch for API testing
global.fetch = vi.fn();
// Mock window.scrollTo
Object.defineProperty(window, 'scrollTo', {
value: vi.fn(),
writable: true
});
// Mock IntersectionObserver
global.IntersectionObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn()
}));
// Mock ResizeObserver
global.ResizeObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn()
}));
// Mock matchMedia
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn()
}))
});
// Mock clipboard API
Object.defineProperty(navigator, 'clipboard', {
value: {
writeText: vi.fn().mockResolvedValue(undefined)
},
writable: true
});
// Mock document.body.style
Object.defineProperty(document.body, 'style', {
value: {
paddingRight: ''
},
writable: true
});