I'm using creat-react-app , and want to use jest for testing.
But, It throws an error about matchMedia
Below this picture is my default test js


Did you try to include the polyfill as the error suggests?
I add a test-setup.js in /src
the test-setup.js is
window.matchMedia = window.matchMedia || function() {
return {
matches : false,
addListener : function() {},
removeListener: function() {}
};
};
and add <rootDir>/src/test-setup.js in setupFiles of jest in package.json
so the error is gone.
Closing as self-answered.
You might want to use an actual polyfill tho (https://github.com/paulirish/matchMedia.js/).
For people who haven't ejected, defining polyfills in a file called src/setupTests.js should work.
Most helpful comment
For people who haven't ejected, defining polyfills in a file called
src/setupTests.jsshould work.