Skip to content

Commit

Permalink
tests: fix window.matchMedia is not a function
Browse files Browse the repository at this point in the history
  • Loading branch information
ptamarit committed May 17, 2024
1 parent 89834c3 commit 0cf130d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,18 @@
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import "@testing-library/jest-dom/extend-expect";

//Mocking matchMedia: https://jestjs.io/docs/en/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
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(),
})),
});

0 comments on commit 0cf130d

Please sign in to comment.