mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-30 06:17:06 +00:00
Node 25+ exposes its own localStorage/sessionStorage globals, which shadow jsdom's in the Vitest environment. Tests that spy on Storage.prototype or pass localStorage to StorageEvent then fail (5 tests in markdown-a and sessionLastSeen). Bind both globals to the jsdom window's storage instead of the memory fallback that only kicked in when methods were missing. via [HAPI](https://hapi.run) Co-authored-by: HAPI <noreply@hapi.run>
56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
import '@testing-library/jest-dom/vitest'
|
|
import { cleanup } from '@testing-library/react'
|
|
import { afterEach } from 'vitest'
|
|
|
|
afterEach(() => {
|
|
cleanup()
|
|
})
|
|
|
|
// Node 25+ exposes its own Web Storage globals, which shadow jsdom's. Tests spy on
|
|
// Storage.prototype and pass localStorage to StorageEvent, so both must be jsdom's.
|
|
const jsdomWindow = (globalThis as typeof globalThis & { jsdom: { window: Window } }).jsdom.window
|
|
for (const name of ['localStorage', 'sessionStorage'] as const) {
|
|
const storage = jsdomWindow[name]
|
|
Object.defineProperty(globalThis, name, { value: storage, configurable: true })
|
|
Object.defineProperty(window, name, { value: storage, configurable: true })
|
|
}
|
|
|
|
if (!('IntersectionObserver' in globalThis)) {
|
|
class MockIntersectionObserver implements IntersectionObserver {
|
|
readonly root = null
|
|
readonly rootMargin = ''
|
|
readonly thresholds = []
|
|
|
|
disconnect() {}
|
|
observe() {}
|
|
takeRecords(): IntersectionObserverEntry[] { return [] }
|
|
unobserve() {}
|
|
}
|
|
|
|
Object.defineProperty(globalThis, 'IntersectionObserver', {
|
|
value: MockIntersectionObserver,
|
|
configurable: true
|
|
})
|
|
Object.defineProperty(window, 'IntersectionObserver', {
|
|
value: MockIntersectionObserver,
|
|
configurable: true
|
|
})
|
|
}
|
|
|
|
if (typeof window.matchMedia !== 'function') {
|
|
Object.defineProperty(window, 'matchMedia', {
|
|
writable: true,
|
|
configurable: true,
|
|
value: (query: string) => ({
|
|
matches: false,
|
|
media: query,
|
|
onchange: null,
|
|
addListener() {},
|
|
removeListener() {},
|
|
addEventListener() {},
|
|
removeEventListener() {},
|
|
dispatchEvent() { return false },
|
|
}),
|
|
})
|
|
}
|