本文介绍了Jest测试中的`requestAnimationFrame` polyfill错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行Jest单元测试时,升级到React后得到了这个错误:

Got this error after upgrading to React when I ran my Jest unit tests:

React depends on requestAnimationFrame. Make sure that you load a polyfill in older browsers.

我该如何解决?

我正在使用Jest 18.1.0.

I'm using Jest 18.1.0.

推荐答案

找到了解决方法!

步骤:

  1. 创建文件__mocks__/react.js
  2. 将以下内容添加到__mocks__/react.js
  1. Create the file __mocks__/react.js
  2. Add the following into __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;
  1. 开玩笑吧!

如对代码的注释所示

这是来自的解决方案 https://github.com/facebook/react/issues/9102#issuecomment-283873039

This is the solution fromhttps://github.com/facebook/react/issues/9102#issuecomment-283873039

这篇关于Jest测试中的`requestAnimationFrame` polyfill错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:08