fix(web): add matchMedia mock to prevent unhandled test errors

The window.matchMedia API (used for accessibility features) was not
mocked in tests, causing unhandled rejections when replay.ts tried to
check prefers-reduced-motion. Added the mock to both test-setup.ts
and the beforeEach hook in replay.test.ts to ensure it's available
before modules load.

Closes: bf-5cwi

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
jedarden 2026-05-25 00:18:32 -04:00
parent aeb2ae7f2d
commit 44f6e5c1ec
2 changed files with 29 additions and 10 deletions

View file

@ -12,6 +12,21 @@ declare global {
describe('replay.ts error handling (URL load button)', () => {
beforeEach(() => {
// Ensure matchMedia is mocked before any module loads
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
// Setup DOM environment
document.body.innerHTML = '<div id="app"></div>';
globalThis.fetch = vi.fn();

View file

@ -1,17 +1,21 @@
/**
* Vitest setup file for browser API mocks.
*/
import { vi } from 'vitest';
// Mock matchMedia for accessibility features in replay.ts
// Must be set up before tests import modules that use it
const matchMediaMock = vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
}));
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
value: matchMediaMock,
});