/** The two globals this module repairs. */ declare const STORAGE_KEYS: readonly ["localStorage", "sessionStorage"]; /** One of the two. */ type WebStorageKey = (typeof STORAGE_KEYS)[number]; /** How {@link restoreWebStorage} finds the window whose storage it trusts. */ interface RestoreWebStorageOptions { /** * The window to take a working storage from, and to repair alongside `globalThis`. * * Defaults to `document.defaultView`, and to nothing where there is no document. Pass `null` to * say there is no window: the repair then does nothing, which is what a DOM-less runtime wants. */ view?: object | null; } /** * Give `globalThis` a `localStorage` and a `sessionStorage` that work. * * Safe to call at any point and as often as you like, and a no-op in the ordinary case: a storage * that survives a write and a read back is left exactly as it is, so a spec's own stub survives * this. It is also a no-op where there is no window — a `node` environment is *supposed* to have no * Web Storage, and inventing one would hand the code under test an API the real runtime lacks. * * The window's own storage is preferred over the stand-in: the environment implements the real * thing, and under a runner whose window is a separate object (a Bun preload, say) only the copy to * `globalThis` failed. The `Map`-backed one is for when nothing else works — which, under Vitest, * is every time, because there `document.defaultView` *is* `globalThis`. * * ```ts * // vitest.setup.ts — or leave it to setupAutoSpy(), which calls this by default * import { restoreWebStorage } from 'vitest-auto-spy/setup'; * * restoreWebStorage(); * ``` * * @returns The keys it had to repair — `[]` when the environment was already sound. */ declare function restoreWebStorage(options?: RestoreWebStorageOptions): WebStorageKey[]; /** Options for {@link stubWebStorage}. */ interface WebStorageStubOptions { /** What the storage holds when it is installed. Values go through `setItem`, so they are coerced. */ items?: Readonly>; /** * The window to install it on as well, when that is a different object from `globalThis` — code * reads `window.localStorage` as often as the bare global. Defaults to `document.defaultView`; * `null` installs on `globalThis` alone. */ view?: object | null; } /** What {@link stubWebStorage} hands back. */ interface WebStorageStub { /** The storage now installed — the same object the global answers until it is restored. */ readonly storage: Storage; /** A copy of what it holds, as a plain record: `expect(local.snapshot()).toEqual({ token: 'abc' })`. */ snapshot(): Record; } /** * Replace `localStorage` or `sessionStorage` with an in-memory `Storage` for this test, empty or seeded. * * ```ts * import { stubWebStorage, type WebStorageStub } from 'vitest-auto-spy/dom-stubs'; * * let local: WebStorageStub; * * beforeEach(() => { * local = stubWebStorage('localStorage', { items: { token: 'abc' } }); * }); * * it('forgets the token on logout', () => { * session.logout(); * expect(local.snapshot()).toEqual({}); * }); * ``` * * `getItem`, `setItem`, `removeItem`, `clear`, `key` and `length` behave as the platform's do, * string coercion included. Installed through `mockValueProp`, so `restoreMockedProps()` — and * therefore `setupAutoSpy()` between tests — puts back whatever the global held before. Install it * in `beforeEach`, like every other stub on this entry. * * Unlike {@link restoreWebStorage} it installs in a `node` environment too: the spec asked for it. */ declare function stubWebStorage(key?: WebStorageKey, options?: WebStorageStubOptions): WebStorageStub; export { type RestoreWebStorageOptions as R, type WebStorageKey as W, type WebStorageStub as a, type WebStorageStubOptions as b, restoreWebStorage as r, stubWebStorage as s };