From 0f0b5bda809c8e4f1a069f9d6fa3b1835ec238a3 Mon Sep 17 00:00:00 2001 From: Patrick Joyce Date: Mon, 26 May 2025 07:25:33 -0400 Subject: [PATCH] fix: mock window.matchMedia for Jest tests --- jest.setup.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/jest.setup.js b/jest.setup.js index 2e550dc7..32fb7cd6 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -10,6 +10,21 @@ if (!global.TextDecoder) { global.TextDecoder = TextDecoder; } +// window.matchMedia doesn't exist in test environment +Object.defineProperty(window, 'matchMedia', { + writable: true, + value: jest.fn().mockImplementation(query => ({ + matches: false, + media: query, + onchange: null, + addListener: jest.fn(), // deprecated + removeListener: jest.fn(), // deprecated + addEventListener: jest.fn(), + removeEventListener: jest.fn(), + dispatchEvent: jest.fn(), + })), +}); + global.React = React; const savedLocation = window.location;