Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

setup.ts

67 lines1.4 KB

1import { vi } from 'vitest'
2​
3// Mock browser APIs that might not be available in test environment
4Object.defineProperty(global, `crypto`, {
5 value: {
6 randomUUID: () => Math.random().toString(36).substring(2, 15),
7 },
8})
9​
10Object.defineProperty(global, `navigator`, {
11 value: {
12 onLine: true,
13 locks: {
14 request: vi.fn().mockResolvedValue(false),
15 },
16 },
17})
18​
19Object.defineProperty(global, `BroadcastChannel`, {
20 value: vi.fn().mockImplementation(() => ({
21 addEventListener: vi.fn(),
22 removeEventListener: vi.fn(),
23 postMessage: vi.fn(),
24 close: vi.fn(),
25 })),
26})
27​
28Object.defineProperty(global, `indexedDB`, {
29 value: {
30 open: vi.fn().mockReturnValue({
31 onerror: null,
32 onsuccess: null,
33 onupgradeneeded: null,
34 result: null,
35 error: new Error(`IndexedDB not available in test environment`),
36 }),
37 },
38})
39​
40Object.defineProperty(global, `localStorage`, {
41 value: {
42 getItem: vi.fn(),
43 setItem: vi.fn(),
44 removeItem: vi.fn(),
45 key: vi.fn(),
46 length: 0,
47 },
48})
49​
50Object.defineProperty(global, `document`, {
51 value: {
52 visibilityState: `visible`,
53 addEventListener: vi.fn(),
54 removeEventListener: vi.fn(),
55 },
56})
57​
58Object.defineProperty(global, `window`, {
59 value: {
60 addEventListener: vi.fn(),
61 removeEventListener: vi.fn(),
62 setTimeout: global.setTimeout,
63 clearTimeout: global.clearTimeout,
64 setInterval: global.setInterval,
65 clearInterval: global.clearInterval,
66 },
67})