-
Notifications
You must be signed in to change notification settings - Fork 0
/
vitest.setup.ts
68 lines (57 loc) · 1.44 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
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";
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",
});
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());
failOnConsole({
shouldFailOnDebug: true,
shouldFailOnError: true,
shouldFailOnInfo: true,
shouldFailOnLog: true,
shouldFailOnWarn: true,
});