在用jest测试含有antd组件的react组件时可能会出现的window.matchMedia is not a function报错

在使用jest测试我写的一个Mylist组件时候,由于Mylist组件使用了antd的组件,导致原先的测试脚本会报错。

错误信息为:window.matchMedia is not a function

解决方案如下:

在根目录下创建jest.setup.js文件。或者从这里下载:https://github.com/ant-design/ant-design/blob/master/tests/setup.js

然后在jest.config.js中加上:setupFiles: ['<rootDir>/jest.setup.js']

像这样:

 

然后再运行就没问题了。

 

posted @ 2020-10-16 15:46  deepCream  阅读(1380)  评论(0)    收藏  举报