-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupTests.ts
More file actions
38 lines (34 loc) · 1.25 KB
/
setupTests.ts
File metadata and controls
38 lines (34 loc) · 1.25 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
import '@testing-library/jest-dom/vitest'
import { Storage as HappyDomStorage } from 'happy-dom'
const BROWSER_STORAGE_PROPERTIES = ['localStorage', 'sessionStorage'] as const
/**
* Ensures Vitest workers use Happy DOM storage when Node exposes a disabled
* experimental storage global that shadows the browser-like implementation.
* @param propertyName Browser storage slot to install on `window` and `globalThis`.
* @returns Nothing; mutates the shared test globals before test files run.
* @example installBrowserStorage('localStorage')
*/
function installBrowserStorage(
propertyName: (typeof BROWSER_STORAGE_PROPERTIES)[number],
): void {
const storage = new HappyDomStorage()
Object.defineProperty(window, propertyName, {
configurable: true,
enumerable: true,
value: storage,
writable: true,
})
Object.defineProperty(globalThis, propertyName, {
configurable: true,
enumerable: true,
value: storage,
writable: true,
})
}
for (const propertyName of BROWSER_STORAGE_PROPERTIES) {
// Node-only test files still load setupFiles, but they do not have a window.
if (typeof window !== 'undefined') {
// Node 24 can expose storage as an unavailable experimental global.
installBrowserStorage(propertyName)
}
}