-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
47 lines (39 loc) · 1.12 KB
/
Copy pathvitest.setup.ts
File metadata and controls
47 lines (39 loc) · 1.12 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
import '@testing-library/jest-dom/vitest';
import { vi } from 'vitest';
// 1. Better Global Mocks using vi.stubGlobal
vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) =>
setTimeout(() => callback(performance.now()), 16)
);
vi.stubGlobal('cancelAnimationFrame', (id: number) => clearTimeout(id));
// 2. Mocking Audio with a clean object
vi.stubGlobal(
'Audio',
vi.fn().mockImplementation(() => ({
play: vi.fn(),
pause: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
}))
);
// 3. Professional Class-based AudioContext Mock
class MockAudioContext {
currentTime = 0;
destination = {};
decodeAudioData = vi.fn();
createOscillator = vi.fn().mockReturnValue({
connect: vi.fn(),
start: vi.fn(),
stop: vi.fn(),
frequency: { value: 0 },
});
createGain = vi.fn().mockReturnValue({
connect: vi.fn(),
gain: {
setValueAtTime: vi.fn(),
exponentialRampToValueAtTime: vi.fn(),
},
});
close = vi.fn().mockResolvedValue(undefined);
}
vi.stubGlobal('AudioContext', MockAudioContext);
vi.stubGlobal('webkitAudioContext', MockAudioContext);