Jest测试中的静态文件

时间:2019-01-20 09:31:13

标签: javascript jestjs jsdom node-canvas

在正在测试的代码中,我正在创建如下图像:

const image = new Image();
image.addEventListener("load", () => {});
image.addEventListener("error", () => {});
image.src = "hello.png"

当我在浏览器中运行时,我已经通过webpack将图像复制到dist目录,并且可以正常工作。开玩笑,但是我得到错误Error: Could not load img: "http://localhost/hello.png"

有关如何在Jest中“提供”静态图像的任何指导?相对于我的测试文件,图像应位于何处?

(请注意,我在玩笑的配置中添加了testEnvironmentOptions: { resources: 'usable' }

1 个答案:

答案 0 :(得分:0)

找到了解决方案:将testURL设置为本地端口,然后在该端口上使用http-server / express或其他类似方式提供文件。

相关问题