Jest test fails : TypeError: window.matchMedia is not a function
Full error 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. I go through Jest documentation, I found the "Manual mocks" section, but I have not any idea about how to do that yet.
Solutionsource: stackoverflow \u2197
The Jest documentation now has an "official" workaround: 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(), })), }); Mocking methods which are not implemented in JSDOM
API access
Get this solution programmatically \u2014 free, no authentication.
curl https://depscope.dev/api/error/bf6f00c35b2f9f92b2306beb300af77b49090939446e749a4be4de5c791ad171hash \u00b7 bf6f00c35b2f9f92b2306beb300af77b49090939446e749a4be4de5c791ad171