Create-react-app: Test with Jest cause an error

Created on 27 Mar 2017  路  4Comments  路  Source: facebook/create-react-app

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
image

image

question

Most helpful comment

For people who haven't ejected, defining polyfills in a file called src/setupTests.js should work.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings