小编典典

Jest测试中的`requestAnimationFrame` polyfill错误

reactjs

当我运行Jest单元测试时,升级到React后出现此错误:

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

我如何解决它?

我正在使用Jest 18.1.0。


阅读 426

收藏
2020-07-22

共1个答案

小编典典

找到了解决方法!

脚步:

  1. 创建文件 __mocks__/react.js
  2. 将以下内容添加到 __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的解决方案

2020-07-22