-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvitest.setup.ts
81 lines (68 loc) · 1.78 KB
/
vitest.setup.ts
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
import { server } from './src/mocks/msw/node'
import * as testingLibraryMatchers from '@testing-library/jest-dom/matchers'
import '@testing-library/jest-dom/vitest'
import { cleanup } from '@testing-library/react'
import { afterEach, expect, beforeAll, afterAll, vi } from 'vitest'
import failOnConsole from 'vitest-fail-on-console'
import { client } from './src/api/generated/sdk.gen'
class MockEventSource {
onmessage: ((event: MessageEvent) => void) | null = null
constructor() {}
close() {}
triggerMessage(data: string) {
if (this.onmessage) {
this.onmessage({ data } as MessageEvent)
}
}
}
expect.extend(testingLibraryMatchers)
afterEach(() => {
cleanup()
})
beforeAll(() => {
server.listen({
onUnhandledRequest: 'error',
})
client.setConfig({
baseUrl: 'https://mock.codegate.ai',
fetch,
})
global.window.matchMedia = vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
}))
global.EventSource = MockEventSource as unknown as typeof EventSource
global.ResizeObserver = class ResizeObserver {
disconnect() {
// do nothing
}
observe() {
// do nothing
}
unobserve() {
// do nothing
}
}
})
afterEach(() => {
server.resetHandlers()
vi.clearAllMocks()
})
afterAll(() => server.close())
const SILENCED_MESSAGES = [
'Not implemented: navigation (except hash changes)', // JSDom issue — can safely be ignored
]
failOnConsole({
shouldFailOnDebug: false,
shouldFailOnError: true,
shouldFailOnInfo: false,
shouldFailOnLog: false,
shouldFailOnWarn: true,
silenceMessage: (message: string) => {
return SILENCED_MESSAGES.some((m) => message.includes(m))
},
})