From 60c6247ef9a11f14ec94981ede252ce74b13f3d7 Mon Sep 17 00:00:00 2001 From: michioxd Date: Sat, 13 Apr 2024 23:42:13 +0700 Subject: [PATCH] Fixed `TypeError: window.matchMedia is not a function` during testing via vitest. --- ui/src/setupTests.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ui/src/setupTests.ts b/ui/src/setupTests.ts index ff7f676..1474276 100644 --- a/ui/src/setupTests.ts +++ b/ui/src/setupTests.ts @@ -7,3 +7,18 @@ // expect(element).toHaveTextContent(/react/i) // learn more: https://github.com/testing-library/jest-dom import "@testing-library/jest-dom"; +import { vi } from 'vitest' + +Object.defineProperty(window, 'matchMedia', { + writable: true, + value: vi.fn().mockImplementation(query => ({ + matches: false, + media: query, + onchange: null, + addListener: vi.fn(), // deprecated + removeListener: vi.fn(), // deprecated + addEventListener: vi.fn(), + removeEventListener: vi.fn(), + dispatchEvent: vi.fn(), + })), +}) \ No newline at end of file