vitest-dom
Use vitest + jsdom for fast, lightweight unit tests for front-end apps
When & Why to Use This Skill
This Claude skill enables developers to implement high-performance front-end testing using Vitest and jsdom. It streamlines the creation of lightweight integration tests that verify real DOM mutations and UI wiring without the overhead of a full browser, ensuring robust code quality and faster development cycles through efficient mocking and deterministic timer management.
Use Cases
- Automated UI Testing: Verify complex DOM updates and component interactions in a simulated browser environment using jsdom to ensure refactors don't break user interface wiring.
- Deterministic Async Testing: Utilize fake timers to test time-dependent logic such as timeouts and intervals without waiting for real-time execution, ensuring fast and reliable test suites.
- API & Side-Effect Mocking: Stub external network requests (window.fetch) and spy on browser side-effects like console logs or clipboard actions to isolate component behavior.
- CI/CD Integration: Configure lightweight test runners that execute rapidly in continuous integration pipelines, catching regressions before code is published.
| name | vitest-dom |
|---|---|
| description | Use vitest + jsdom for fast, lightweight unit tests for front-end apps |
- 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.
- Log browser
console.*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 for each test to isolate
window,document, etc. - 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. - Add timeouts per test case, e.g.
{ timeout: 10_000 }, for long-running tests.