-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-setup.js
More file actions
139 lines (121 loc) · 3.23 KB
/
vite-setup.js
File metadata and controls
139 lines (121 loc) · 3.23 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @see https://github.com/davipon/svelte-component-test-recipes
*/
import * as matchers from "@testing-library/jest-dom/matchers";
import { expect, vi } from "vitest";
import { readable } from "svelte/store";
import { ResizeObserver } from "@juggle/resize-observer";
import "jsdom-worker";
import {
IntersectionObserver,
MediaQueryList,
MediaQueryListEvent,
} from "./src/lib/dusk/mocks";
/*
* Mocking deprecated `atob` and `btoa` functions in Node.
* Vitest get stuck otherwise.
*/
vi.spyOn(global, "atob").mockImplementation((data) =>
Buffer.from(data, "base64").toString("binary")
);
vi.spyOn(global, "btoa").mockImplementation((data) =>
Buffer.from(data, "binary").toString("base64")
);
// Adding missing bits in JSDOM
vi.mock("./src/lib/dusk/mocks/IntersectionObserver");
global.IntersectionObserver = IntersectionObserver;
global.ResizeObserver = ResizeObserver;
global.MediaQueryListEvent = MediaQueryListEvent;
global.MediaQueryList = MediaQueryList;
const elementMethods = ["scrollBy", "scrollTo", "scrollIntoView"];
elementMethods.forEach((method) => {
if (!Element.prototype[method]) {
Object.defineProperty(Element.prototype, method, {
value: () => {},
writable: true,
});
}
});
// Needed by Svelte's animation helpers (e.g. `animate:flip`) which use the Web Animations API.
if (!Element.prototype.getAnimations) {
Object.defineProperty(Element.prototype, "getAnimations", {
value: () => [],
writable: true,
});
}
// Add custom jest matchers
expect.extend(matchers);
// Mock SvelteKit runtime module $app/environment
vi.mock("$app/environment", () => ({
browser: false,
building: false,
dev: true,
version: "any",
}));
// Mock app paths
vi.mock("$app/paths", async (importOriginal) => ({
...(await importOriginal()),
get base() {
return "/some-base-path";
},
}));
// Mock SvelteKit runtime module $app/navigation
vi.mock("$app/navigation", () => ({
afterNavigate: () => {},
beforeNavigate: () => {},
disableScrollHandling: () => {},
goto: () => Promise.resolve(),
invalidate: () => Promise.resolve(),
invalidateAll: () => Promise.resolve(),
preloadCode: () => Promise.resolve(),
preloadData: () => Promise.resolve(),
}));
// Mock SvelteKit runtime module $app/stores
vi.mock("$app/stores", () => {
const getStores = () => {
const navigating = readable(null);
const page = readable({
data: {},
error: null,
form: undefined,
params: {},
route: {
id: null,
},
status: 200,
url: new URL("http://localhost"),
});
const updated = {
check: async () => false,
subscribe: readable(false).subscribe,
};
return { navigating, page, updated };
};
const page = {
subscribe(fn) {
return getStores().page.subscribe(fn);
},
};
const navigating = {
subscribe(fn) {
return getStores().navigating.subscribe(fn);
},
};
const updated = {
check: async () => false,
subscribe(fn) {
return getStores().updated.subscribe(fn);
},
};
return {
getStores,
navigating,
page,
updated,
};
});
// Define matchMedia property
Object.defineProperty(window, "matchMedia", {
value: (query) => new MediaQueryList(query, false),
writable: true,
});