31 lines
876 B
TypeScript
31 lines
876 B
TypeScript
// Node ≥ 25 имеет собственный экспериментальный localStorage, который затеняет
|
|
// jsdom и без --localstorage-file не работает. Подставляем простую реализацию.
|
|
class MemoryStorage implements Storage {
|
|
private map = new Map<string, string>()
|
|
get length() {
|
|
return this.map.size
|
|
}
|
|
clear() {
|
|
this.map.clear()
|
|
}
|
|
getItem(key: string) {
|
|
return this.map.get(key) ?? null
|
|
}
|
|
key(index: number) {
|
|
return [...this.map.keys()][index] ?? null
|
|
}
|
|
removeItem(key: string) {
|
|
this.map.delete(key)
|
|
}
|
|
setItem(key: string, value: string) {
|
|
this.map.set(key, String(value))
|
|
}
|
|
}
|
|
|
|
if (typeof globalThis.localStorage?.clear !== 'function') {
|
|
Object.defineProperty(globalThis, 'localStorage', {
|
|
value: new MemoryStorage(),
|
|
configurable: true,
|
|
})
|
|
}
|