/** * @file test-setup.ts * @description Vitest global setup for the React client test suite. Pulls in * jest-dom matchers, initializes the real i18n bundle (same as production), * forces English before each test for deterministic assertions, and runs * Testing Library cleanup after every test to prevent DOM leakage between cases. * * Imported from `vitest.config.ts` via `setupFiles`. * * @author Nguyễn Ngọc Trí Vĩ */ import "@testing-library/jest-dom/vitest"; import { cleanup } from "@testing-library/react"; import { afterEach, beforeEach } from "vitest"; import { i18nReady } from "./i18n/index"; import i18n from "i18next"; // Locales load lazily in the app; tests get both up front so every // `changeLanguage` (en or vi) switches synchronously, as before. await i18nReady; await i18n.loadLanguages(["en", "vi"]); /** jsdom does not implement ResizeObserver — stub it for components that use it. */ // @ts-expect-error global stub global.ResizeObserver = class { observe() {} disconnect() {} }; /** jsdom implements no scrolling — stub the methods the tab bars call. */ for (const fn of ["scrollIntoView", "scrollBy", "scrollTo"] as const) { if (!(Element.prototype as unknown as Record)[fn]) { (Element.prototype as unknown as Record)[fn] = function () {}; } } /** Pin locale to English — LanguageDetector may otherwise pick up zh/vi from the host OS. */ beforeEach(() => { i18n.changeLanguage("en"); }); /** Unmount rendered trees and reset the document between tests. */ afterEach(() => { cleanup(); });