Files
apparel-designer/src/test/setup.js
2026-05-23 03:28:58 -05:00

46 lines
2.1 KiB
JavaScript

// Vitest setup file — runs once per test file, before any test.
//
// Two responsibilities:
//
// 1. Register @testing-library/jest-dom's custom matchers so tests
// can use `expect(node).toBeInTheDocument()`, `toHaveTextContent()`,
// `toHaveAttribute()`, etc. Without this import, those matchers
// throw "expected jest-dom matcher" at assertion time.
//
// 2. Run RTL's `cleanup()` after every test. RTL renders into a
// shared DOM container; without explicit cleanup, components
// from one test stick around for the next, producing
// false-positive "found multiple instances" errors and
// memory-grow bugs in long suites. Vitest with `globals: true`
// exposes `afterEach` so the import below works without
// naming it on the import side.
//
// What's NOT here (intentionally)
// ─────────────────────────────────
// • localStorage clearing. jsdom gives each test file a fresh
// localStorage, and our tests that touch localStorage (history
// debug flag) explicitly set+clear their own keys per test.
// A blanket `localStorage.clear()` in afterEach would mask
// setup bugs in tests that depend on a clean key.
//
// • Konva mocking. We don't yet have tests that mount React-Konva
// components — those will need Stage/Layer stubs and will live
// in a follow-up batch once we know the test setup story for
// canvas-dependent code. For pure utility and hook tests
// covered in this initial batch, Konva isn't loaded.
//
// • Timer mocking globals. Individual tests that need
// `vi.useFakeTimers()` (debounce tests for updateElement, the
// auto-dismiss toast timer, etc.) call it themselves and pair
// with `vi.useRealTimers()` in their own afterEach. Keeping
// it test-local rather than global makes the timing intent
// visible at the test site.
import '@testing-library/jest-dom/vitest';
import { afterEach } from 'vitest';
import { cleanup } from '@testing-library/react';
afterEach(() => {
cleanup();
});