-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
58 lines (52 loc) · 1.82 KB
/
Copy pathvitest.setup.ts
File metadata and controls
58 lines (52 loc) · 1.82 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
/* eslint-disable import/order --
prettier-plugin-organize-imports owns import ordering and forces the
side-effect import first, which conflicts with this rule's newlines-between check. */
import "@testing-library/jest-dom/vitest";
import { configure } from "@testing-library/svelte";
import { TextDecoder, TextEncoder } from "util";
import { IntersectionObserverPassive } from "./src/tests/lib/mocks/infinitescroll.mock";
// jsdom does not implement TextEncoder/TextDecoder, so polyfill them with node's.
// The `global as {...}` cast is needed because TS 6 made `Uint8Array` generic.
(global as { TextEncoder: typeof globalThis.TextEncoder }).TextEncoder =
TextEncoder;
(global as { TextDecoder: typeof TextDecoder }).TextDecoder = TextDecoder;
(
global as { IntersectionObserver: typeof IntersectionObserver }
).IntersectionObserver = IntersectionObserverPassive;
// We mock ResizeObserver and element.animate because neither JSDOM nor Happy DOM supports them, while Svelte v5 requires them.
// Interesting related thread: https://github.com/testing-library/svelte-testing-library/issues/284
global.ResizeObserver = class ResizeObserver {
observe() {
// do nothing
}
unobserve() {
// do nothing
}
disconnect() {
// do nothing
}
};
Element.prototype.animate = vi
.fn()
.mockImplementation(() => ({ cancel: vi.fn(), finished: Promise.resolve() }));
// testing-library setup
configure({
testIdAttribute: "data-tid",
});
// make `DOMPurify` available for unit tests
import DOMPurify from "dompurify";
const purify = DOMPurify();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: used for testing only
global.DOMPurify = purify;
global.ResizeObserver = class ResizeObserver {
observe() {
// do nothing
}
unobserve() {
// do nothing
}
disconnect() {
// do nothing
}
};