Files
hapi/web/src/test/setup.ts
T
9e85428913 test(web): use jsdom Web Storage under Node 25+ (#2438)
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>
2026-11-15 07:59:57 +08:00

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 },
}),
})
}