platform-codebase/test-setup.ts

44 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

/**
* Test setup for bun test runner
*
* Provides DOM environment (happy-dom) for React component tests
* and global test utilities.
*/
import { GlobalRegistrator } from '@happy-dom/global-registrator';
// Register happy-dom globals (window, document, etc.)
GlobalRegistrator.register();
// Provide localStorage mock for tests that need it
if (typeof globalThis.localStorage === 'undefined') {
const storage = new Map<string, string>();
globalThis.localStorage = {
getItem: (key: string) => storage.get(key) ?? null,
setItem: (key: string, value: string) => storage.set(key, value),
removeItem: (key: string) => storage.delete(key),
clear: () => storage.clear(),
get length() {
return storage.size;
},
key: (index: number) => [...storage.keys()][index] ?? null,
};
}
// Provide sessionStorage mock
if (typeof globalThis.sessionStorage === 'undefined') {
const storage = new Map<string, string>();
globalThis.sessionStorage = {
getItem: (key: string) => storage.get(key) ?? null,
setItem: (key: string, value: string) => storage.set(key, value),
removeItem: (key: string) => storage.delete(key),
clear: () => storage.clear(),
get length() {
return storage.size;
},
key: (index: number) => [...storage.keys()][index] ?? null,
};
}