| name | vitest-dom |
| description | Fast, lightweight testing for front-end apps. Uses vitest + jsdom instead to avoid heavy playwright. |
- Use vitest and jsdom for front-end testing.
- Avoid
vitest.config.*; default ESM import works, launch via"test": "npx -y vitest run"inpackage.json. Addjsdomas adevDependency. Addnpm testtoprepublishOnly - Treat tests as lightweight integration, not unit. Load the full HTML + scripts and verify real DOM mutations; ensures refactors don't silently break UI wiring.
- Share one
Browserper test suite to cut startup time:new Browser({console, settings}). Log browserconsole.*output. - Mount local HTML.
settings.fetch.virtualServers = [{url:"https://test/", directory: <root>}]. Usepage.goto("https://test/...")to load files without a dev-server. - Create a fresh
page = browser.newPage()for each test to isolatewindow,document, etc. await page.waitUntilComplete()afterpage.goto()ensures all inline & async scripts executed before assertions.- Fake timers for deterministic testing.
- Call
vi.useFakeTimers()inbeforeAll,vi.useRealTimers()inafterAll. - Re-bind
window.setTimeout = setTimeoutso app code sees the mocked clock. - Drive async paths with
vi.advanceTimersByTime(ms)instead ofawait sleep.
- Call
- Stub external APIs with
vi.fn()- e.g.window.fetch = vi.fn(() => Promise.resolve({ok:true,...}))avoids network and lets you assert payloads. - Spy on side-effects -
vi.spyOn(console, "error"), clipboard reads, etc.; alwaysmockRestore()afterwards to prevent bleed-through. - Build specialised browsers (
new Browser({device:{prefersColorScheme:"dark"}})) to test colour-scheme logic. - Drive UI through real DOM events.
element.click()/dispatchEvent(new window.Event("input",{bubbles:true}))instead of directly calling handlers; matches user behaviour. - Add timeouts per test case, e.g.
{ timeout: 10_000 }, for long-running tests.