52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import '@testing-library/jest-dom/vitest';
|
|
import { afterEach } from 'vitest';
|
|
import { cleanup } from '@testing-library/react';
|
|
|
|
// recharts (ResponsiveContainer) requires ResizeObserver, which jsdom does not provide.
|
|
class ResizeObserverStub {
|
|
observe() {}
|
|
unobserve() {}
|
|
disconnect() {}
|
|
}
|
|
if (typeof globalThis.ResizeObserver === 'undefined') {
|
|
Object.defineProperty(globalThis, 'ResizeObserver', {
|
|
value: ResizeObserverStub,
|
|
configurable: true,
|
|
writable: true,
|
|
});
|
|
}
|
|
|
|
// vitest runs with globals:false, so RTL auto-cleanup is not registered
|
|
// automatically; clean the DOM after every test.
|
|
afterEach(() => {
|
|
cleanup();
|
|
});
|
|
|
|
// Node 25 exposes a built-in `localStorage` global whose methods are unavailable
|
|
// without a valid --localstorage-file, and vitest's jsdom environment does not
|
|
// copy window.localStorage (a prototype property) onto globalThis.
|
|
// Provide a working in-memory implementation before app code reads it at import time.
|
|
const memory = new Map<string, string>();
|
|
const storage: Storage = {
|
|
getItem: (key: string) => (memory.has(key) ? memory.get(key)! : null),
|
|
setItem: (key: string, value: string) => {
|
|
memory.set(key, String(value));
|
|
},
|
|
removeItem: (key: string) => {
|
|
memory.delete(key);
|
|
},
|
|
clear: () => {
|
|
memory.clear();
|
|
},
|
|
key: (index: number) => [...memory.keys()][index] ?? null,
|
|
get length() {
|
|
return memory.size;
|
|
},
|
|
};
|
|
|
|
Object.defineProperty(globalThis, 'localStorage', {
|
|
value: storage,
|
|
configurable: true,
|
|
writable: true,
|
|
});
|