import '@testing-library/jest-dom/vitest'; import { vi } from 'vitest'; import { matchMedia } from 'mock-match-media'; /** * Suppress noisy `null` output from libraries in CI. * Some libraries write `null` to stdout/stderr, which Vitest * outputs as "stdout/stderr | unknown test null" messages. */ if (process.env.CI) { const isNullOutput = (chunk: string | Uint8Array) => typeof chunk === 'string' && chunk.trim() === 'null'; const originalStderrWrite = process.stderr.write.bind(process.stderr); process.stderr.write = ( chunk: string | Uint8Array, encodingOrCallback?: BufferEncoding | ((err?: Error) => void), callback?: (err?: Error | null) => void ): boolean => { if (isNullOutput(chunk)) return true; return originalStderrWrite( chunk, encodingOrCallback as BufferEncoding, callback ); }; const originalStdoutWrite = process.stdout.write.bind(process.stdout); process.stdout.write = ( chunk: string | Uint8Array, encodingOrCallback?: BufferEncoding | ((err?: Error) => void), callback?: (err?: Error | null) => void ): boolean => { if (isNullOutput(chunk)) return true; return originalStdoutWrite( chunk, encodingOrCallback as BufferEncoding, callback ); }; } /** * `matchMedia` does not exist in `jsdom`, but is required to be able to import certain Svelte modules. */ globalThis.matchMedia = matchMedia; /** * `Element.animate` does not exist in `jsdom`, but is required to run Svelte transitions. * * Prefer testing element animations in browser mode. */ if (typeof Element.prototype.animate !== 'function') { Element.prototype.animate = () => ({ cancel: vi.fn(), finished: Promise.resolve(), }) as unknown as Animation; } /** * `PointerEvent` does not exist in `jsdom` so this polyfill is based off this * comment on the PR to add it: * * https://github.com/jsdom/jsdom/pull/2666#issuecomment-691216178 */ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (!globalThis.PointerEvent) { class PointerEvent extends MouseEvent { public height: number | undefined; public isPrimary: boolean | undefined; public pointerId: number | undefined; public pointerType: string | undefined; public pressure: number | undefined; public tangentialPressure: number | undefined; public tiltX: number | undefined; public tiltY: number | undefined; public twist: number | undefined; public width: number | undefined; constructor(type: string, params: PointerEventInit = {}) { super(type, params); this.pointerId = params.pointerId; this.width = params.width; this.height = params.height; this.pressure = params.pressure; this.tangentialPressure = params.tangentialPressure; this.tiltX = params.tiltX; this.tiltY = params.tiltY; this.pointerType = params.pointerType; this.isPrimary = params.isPrimary; } } // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any global.PointerEvent = PointerEvent as any; }