-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
36 lines (33 loc) · 1019 Bytes
/
vitest.setup.ts
File metadata and controls
36 lines (33 loc) · 1019 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
26
27
28
29
30
31
32
33
34
35
36
import '@testing-library/jest-dom'
import { afterEach } from 'vitest'
import { cleanup } from '@testing-library/react'
function makeMemoryStorage() {
const memory = new Map<string, string>()
return {
getItem: (k: string) => memory.get(k) ?? null,
setItem: (k: string, v: string) => void memory.set(k, String(v)),
removeItem: (k: string) => void memory.delete(k),
clear: () => memory.clear(),
key: (i: number) => Array.from(memory.keys())[i] ?? null,
get length() {
return memory.size
},
}
}
// Ensure window.localStorage / sessionStorage have working methods.
// vitest 4's jsdom environment may register them as objects without methods.
if (typeof window !== 'undefined') {
Object.defineProperty(window, 'localStorage', {
value: makeMemoryStorage(),
configurable: true,
writable: false,
})
Object.defineProperty(window, 'sessionStorage', {
value: makeMemoryStorage(),
configurable: true,
writable: false,
})
}
afterEach(() => {
cleanup()
})