-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvitest-setup.ts
More file actions
61 lines (50 loc) · 1.69 KB
/
Copy pathvitest-setup.ts
File metadata and controls
61 lines (50 loc) · 1.69 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
import '@testing-library/jest-dom'
import { vi } from 'vitest'
import createFetchMock from 'vitest-fetch-mock'
import { initReactI18next } from 'react-i18next'
import i18n from 'i18next'
import en from './src/i18n/locales/en.json'
// Mock WebSocket globally for all tests using a class
class MockWebSocket {
static CONNECTING = 0
static OPEN = 1
static CLOSING = 2
static CLOSED = 3
CONNECTING = MockWebSocket.CONNECTING
OPEN = MockWebSocket.OPEN
CLOSING = MockWebSocket.CLOSING
CLOSED = MockWebSocket.CLOSED
readyState = MockWebSocket.OPEN
close = vi.fn()
send = vi.fn()
addEventListener = vi.fn()
removeEventListener = vi.fn()
onopen: ((event: Event) => void) | null = null
onclose: ((event: CloseEvent) => void) | null = null
onmessage: ((event: MessageEvent) => void) | null = null
onerror: ((event: Event) => void) | null = null
constructor(_url: string) {
// Constructor for WebSocket mock
}
}
global.WebSocket = MockWebSocket as unknown as typeof WebSocket
// Mock IntersectionObserver globally using a class
class MockIntersectionObserver {
observe = vi.fn()
unobserve = vi.fn()
disconnect = vi.fn()
constructor(_callback: IntersectionObserverCallback, _options?: IntersectionObserverInit) {
// Constructor for IntersectionObserver mock
}
}
global.IntersectionObserver = MockIntersectionObserver as unknown as typeof IntersectionObserver
const fetchMocker = createFetchMock(vi)
// sets globalThis.fetch and globalThis.fetchMock to our mocked version
fetchMocker.enableMocks()
// Internationalization
i18n.use(initReactI18next).init({
resources: { en: { translation: en } },
lng: 'en',
fallbackLng: 'en',
interpolation: { escapeValue: false },
})