import '@testing-library/jest-dom/vitest'; // Import @testing-library/svelte here so its beforeEach (setup/asyncWrapper=act) // is registered before our own beforeEach below. import '@testing-library/svelte'; import { configure } from '@testing-library/dom'; import { vi } from 'vitest'; import { tick } from 'svelte'; import userEvent from '@testing-library/user-event'; // Patch userEvent direct-API methods to use delay:null when fake timers are // active. With delay:null, user-event's internal wait() short-circuits // (typeof null !== 'number') and no setTimeout is scheduled — so clicks and // other interactions work correctly under vi.useFakeTimers(). const originalClick = userEvent.click.bind(userEvent); // @ts-expect-error patching direct API userEvent.click = (element: Element, options = {}) => { if (vi.isFakeTimers()) { // @ts-expect-error delay:null is a valid user-event option return originalClick(element, { delay: null, ...options }); } return originalClick(element, options); }; // Also update asyncWrapper to call tick() after async operations so Svelte // DOM updates are flushed. @testing-library/svelte's act() already does this, // but we re-configure after it to preserve our fake-timer behaviour. beforeEach(() => { configure({ asyncWrapper: async (fn: () => Promise) => { const result = await fn(); await tick(); return result; } }); });