-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest-setup.ts
More file actions
25 lines (23 loc) · 982 Bytes
/
Copy pathvitest-setup.ts
File metadata and controls
25 lines (23 loc) · 982 Bytes
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
import { beforeEach, vi } from 'vitest';
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem: (key: string) => store[key] || null,
setItem: (key: string, value: string) => { store[key] = value.toString(); }, // Ensure value is string
removeItem: (key: string) => { delete store[key]; },
clear: () => { store = {}; },
key: (index: number) => Object.keys(store)[index] || null,
get length() { return Object.keys(store).length; }
};
})();
beforeEach(() => {
localStorageMock.clear();
vi.stubGlobal('localStorage', localStorageMock);
// Mock crypto.randomUUID if not available in happy-dom/jsdom
if (typeof crypto === 'undefined' || !crypto.randomUUID) {
vi.stubGlobal('crypto', {
...global.crypto, // spread existing crypto if any
randomUUID: () => Math.random().toString(36).substring(2) + Date.now().toString(36)
});
}
});