import { afterAll } from "bun:test"; /** * Restore globals a suite replaces, once the suite is done. * * These suites install a happy-dom window over the real globals and delete * them before each test so every test starts clean. bun test loads and runs * one file at a time rather than importing them all up front, so anything left * deleted is still missing when the next suite runs -- which is how `bun test` * with no argument came to fail unrelated files with "fetch is not a * function". Names absent at capture time are deleted again rather than being * restored as undefined, so a global that never existed does not gain a key. */ export function restoreGlobalsAfterAll(names: readonly string[]): void { const captured = new Map( names.map((name) => [name, (globalThis as Record)[name]]), ); afterAll(() => { for (const [name, value] of captured) { if (value === undefined) { delete (globalThis as Record)[name]; } else { (globalThis as Record)[name] = value; } } }); }