Fixed TypeError: window.matchMedia is not a function during testing via vitest.

This commit is contained in:
michioxd 2024-04-13 23:42:13 +07:00 committed by Scott Lamb
parent 317b8e9484
commit 60c6247ef9

View File

@ -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(),
})),
})