-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvitest.setup.ts
43 lines (38 loc) · 1.52 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
import { fetch, Request, Response } from '@remix-run/web-fetch';
import { vi } from 'vitest';
if (!globalThis.fetch) {
// Built-in lib.dom.d.ts expects `fetch(Request | string, ...)` but the web
// fetch API allows a URL so @remix-run/web-fetch defines
// `fetch(string | URL | Request, ...)`
// @ts-expect-error not sure why we need this...
globalThis.fetch = fetch;
// Same as above, lib.dom.d.ts doesn't allow a URL to the Request constructor
// @ts-expect-error not sure why we need this...
globalThis.Request = Request;
// web-std/fetch Response does not currently implement Response.error()
// @ts-expect-error not sure why we need this...
globalThis.Response = Response;
}
// https://github.com/remix-run/react-router/blob/main/packages/router/__tests__/setup.ts
// Mock matchMedia method as its not implemented in vitest
// https://github.com/vitest-dev/vitest/issues/821
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
// Mocking use-resize-observer which is used by nuka-carousel
const ResizeObserverMock = vi.fn(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));
vi.stubGlobal('ResizeObserver', ResizeObserverMock);