{"id":649,"hash":"bf6f00c35b2f9f92b2306beb300af77b49090939446e749a4be4de5c791ad171","pattern":"Jest test fails : TypeError: window.matchMedia is not a function","full_message":"This is my first front-end testing experience. In this project, I'm using Jest snapshot testing and got an error TypeError: window.matchMedia is not a function inside my component.\n\nI go through Jest documentation, I found the \"Manual mocks\" section, but I have not any idea about how to do that yet.","ecosystem":"npm","package_name":"reactjs","package_version":null,"solution":"The Jest documentation now has an \"official\" workaround:\n\nObject.defineProperty(window, 'matchMedia', {\n  writable: true,\n  value: jest.fn().mockImplementation(query => ({\n    matches: false,\n    media: query,\n    onchange: null,\n    addListener: jest.fn(), // Deprecated\n    removeListener: jest.fn(), // Deprecated\n    addEventListener: jest.fn(),\n    removeEventListener: jest.fn(),\n    dispatchEvent: jest.fn(),\n  })),\n});\n\nMocking methods which are not implemented in JSDOM","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/39830580/jest-test-fails-typeerror-window-matchmedia-is-not-a-function","votes":236,"created_at":"2026-04-19T04:51:27.257767+00:00","updated_at":"2026-04-19T04:51:27.257767+00:00"}