-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathvitest.setup.js
More file actions
70 lines (66 loc) · 1.94 KB
/
Copy pathvitest.setup.js
File metadata and controls
70 lines (66 loc) · 1.94 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
import '@testing-library/jest-dom/vitest';
import { afterEach } from 'vitest';
import { cleanup } from '@testing-library/react';
afterEach(() => {
cleanup();
});
if (typeof window !== 'undefined' && !window.matchMedia) {
window.matchMedia = (query) => ({
matches: false,
media: query,
onchange: null,
addListener: () => {},
removeListener: () => {},
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => false,
});
}
if (typeof window !== 'undefined' && !window.ResizeObserver) {
window.ResizeObserver = class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
};
}
// localStorage is inconsistent across Node/jsdom/Vitest combos: depending on
// the runtime, `window.localStorage` may lack methods, or expose methods that
// silently no-op (e.g. Node 22's experimental localStorage without
// `--localstorage-file` drops every write). Method-existence checks therefore
// aren't enough — Node 22 has the methods but they don't persist, which made
// the persistence tests pass on Node 26 yet fail in CI on Node 22. Install a
// spec-shaped in-memory polyfill unconditionally so storage behaves
// identically everywhere.
if (typeof window !== 'undefined') {
class MemoryStorage {
constructor() {
this._store = new Map();
}
get length() {
return this._store.size;
}
clear() {
this._store.clear();
}
getItem(key) {
return this._store.has(key) ? this._store.get(key) : null;
}
setItem(key, value) {
this._store.set(String(key), String(value));
}
removeItem(key) {
this._store.delete(key);
}
key(index) {
return Array.from(this._store.keys())[index] ?? null;
}
}
Object.defineProperty(window, 'localStorage', {
configurable: true,
value: new MemoryStorage(),
});
Object.defineProperty(window, 'sessionStorage', {
configurable: true,
value: new MemoryStorage(),
});
}