我尝试为我现有的项目创建测试文件,该项目是由
构建的https://github.com/facebookincubator/create-react-app
但是当我跑npm test a
FAIL src/__tests__/movie_single/MovieSingle-test.js
● Test suite failed to run
matchMedia not present, legacy browsers require a polyfill
at Object.MediaQueryDispatch (node_modules/enquire.js/dist/enquire.js:226:19)
at node_modules/enquire.js/dist/enquire.js:291:9
at Object.<anonymous>.i (node_modules/enquire.js/dist/enquire.js:11:20)
at Object.<anonymous> (node_modules/enquire.js/dist/enquire.js:21:2)
at Object.<anonymous> (node_modules/react-responsive-mixin/index.js:2:28)
at Object.<anonymous> (node_modules/react-slick/lib/slider.js:19:29)
at Object.<anonymous> (node_modules/react-slick/lib/index.js:3:18)
at Object.<anonymous> (src/components/movie_single/MovieDetailsBox.js:4:45)
at Object.<anonymous> (src/components/movie_single/MovieSingle.js:3:50)
at Object.<anonymous> (src/__tests__/movie_single/MovieSingle-test.js:3:46)
at process._tickCallback (internal/process/next_tick.js:103:7)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 2.642s
Ran all test suites matching "a".
所以这是我的package.json
{
"name": "xxx",
"version": "0.1.0",
"private": true,
"devDependencies": {
"autoprefixer-stylus": "0.10.0",
"concurrently": "3.0.0",
"react-scripts": "0.6.1",
"stylus": "0.54.5"
},
"scripts": {
"start": "react-scripts start",
"watch": "concurrently --names 'webpack, stylus' --prefix name 'npm run start' 'npm run styles:watch'",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
}
}
这是我的MovieSingle-test.js
import React from 'react';
import ReactDOM from 'react-dom';
import MoviesSingle from '../../components/movie_single/MovieSingle';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<MoviesSingle />, div);
});
那么如何解决这个问题并至少通过反应组件测试?
谢谢!
答案 0 :(得分:6)
在项目中添加src/setupTests.js
(将在运行测试之前执行):
/**
* fix: `matchMedia` not present, legacy browsers require a polyfill
*/
global.matchMedia = global.matchMedia || function() {
return {
matches : false,
addListener : function() {},
removeListener: function() {}
}
}
答案 1 :(得分:5)
我也碰到了它,这是帮助我的原因:
从here获取test-setup.js
并将其放入src
目录
window.matchMedia = window.matchMedia || function() {
return {
matches : false,
addListener : function() {},
removeListener: function() {}
};
};
将以下内容添加到package.json
"jest": {
"setupFiles": ["<rootDir>\\src\\test-setup.js", "<rootDir>\\config\\polyfills.js"]
},
看看是否有帮助。
答案 2 :(得分:1)
如果您使用的是GatsbyJS,请将@maow中的代码放在根目录中的loaderershim.js文件中。
答案 3 :(得分:1)
添加这段代码:
window.matchMedia =
window.matchMedia ||
function() {
return {
matches: false,
addListener: function() {},
removeListener: function() {}
};
};
在jest.setup.js或setupTests.js中(您可以从jest.config.js中检查名称 例如。 setupFiles:[“ ./jest.setup.js”])
答案 4 :(得分:0)
从错误消息中找出线索:
matchMedia不存在,旧版浏览器需要填充
这表示某些功能在测试时不可用,需要以某种方式实现以执行操作。
为matchMedia
找到合适的polyfill代码,例如this polyfill。
然后在名为src
的{{1}}文件夹中创建一个新文件,并将填充代码粘贴到文件中。 React脚本负责在运行测试之前执行此文件。
现在再次运行测试。这是固定的。 Others遇到了同样的问题。
答案 5 :(得分:0)
我按照以下步骤解决了我的问题:
window.matchMedia = window.matchMedia || function() {
return {
matches: false,
addListener: function() {},
removeListener: function() {}
};
};
注意:再次运行 npm 测试,它将解决问题(停止监视任务并重新启动它)。
答案 6 :(得分:0)
这就是我设法让它工作的方式:
// fix: `matchMedia` not present, legacy browsers require a polyfill
global.window.matchMedia = (query) => ({
matches: false,
media: query,
onchange: null,
addListener: () => {}, // deprecated
removeListener: () => {}, // deprecated
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => {},
})