升级到 React 后运行 Jest 单元测试时出现此错误:
React depends on requestAnimationFrame. Make sure that you load a polyfill in older browsers.
我如何解决它?
我正在使用 Jest 18.1.0。
找到了解决方法!
Steps:
- 创建文件
__mocks__/react.js
(参见关于__mocks__文件夹在这里 https://jestjs.io/docs/manual-mocks)
- 将以下内容添加到
__mocks__/react.js
const react = require('react');
// Resolution for requestAnimationFrame not supported in jest error :
// https://github.com/facebook/react/issues/9102#issuecomment-283873039
global.window = global;
window.addEventListener = () => {};
window.requestAnimationFrame = () => {
throw new Error('requestAnimationFrame is not supported in Node');
};
module.exports = react;
- 运行笑话!
正如代码注释中所标记的
这是来自的解决方案https://github.com/facebook/react/issues/9102#issuecomment-283873039 https://github.com/facebook/react/issues/9102#issuecomment-283873039
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)