由于 .getContext() 未实现,单元测试引发错误 [英] unit test raises error because of .getContext() is not implemented

查看:22
本文介绍了由于 .getContext() 未实现,单元测试引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jest 为使用画布元素的组件编写测试.当我运行类似这样的测试时,我不断收到错误消息.

I am writing tests using Jest for components that use canvas elements. I keep getting an error when I run my tests that looks like this.

Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package)

据我所知,Jest 使用 jsdom 进行测试,如果您安装了 canvas 或 canvas-prebuilt 包,那么 jsdom 与 canvas 兼容.

From my understanding Jest uses jsdom for its testing and that jsdom is compatible with canvas if you install the canvas or canvas-prebuilt packages.

我已经尝试安装这些软件包中的每一个,但都没有解决错误.我认为唯一可能出错的是 jsdom 找不到 canvas 或 canvas-prebuilt 包.有谁知道修复此错误或测试以查看 jsdom 是否正在查找其他包的方法?非常感谢!

I have tried installing each of these packages and neither of them have resolved the error. The only thing that I think could be going wrong is that jsdom cannot find the canvas or canvas-prebuilt packages. Does anyone know a way to fix this error or test to see if jsdom is finding the other packages? Thanks a lot!

推荐答案

您可以在 jest 设置脚本

HTMLCanvasElement.prototype.getContext = () => { 
  // return whatever getContext has to return
};

这篇关于由于 .getContext() 未实现,单元测试引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆