image - Expo Jest 找不到类似 'file@2x.png' 的图像

标签 image request automated-tests jestjs expo

如您所知,Expo 通过分析文件后缀来使用自适应图像大小,如下所示: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cf4f4f4ccbff4a2fce2eb" rel="noreferrer noopener nofollow">[email protected]</a> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b3333330b7933653b252c" rel="noreferrer noopener nofollow">[email protected]</a> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="671f1f1f27561f49170900" rel="noreferrer noopener nofollow">[email protected]</a>

我可以像这样声明省略文件末尾的图像:

const iconHomeButton = require('./images/homeButton.png');

文件夹中实际的图像名称是:

<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbd3d4d6def9cecfcfd4d5fb89c395cbd5dc" rel="noreferrer noopener nofollow">[email protected]</a>

在 Expo 上它工作正常,但是当我尝试使用 Jest 测试它时,它找不到图像。 如果我将文件名更改为真实姓名,例如:

const iconHomeButton = require('./images/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="492126242c0b3c3d3d2627097b316739272e" rel="noreferrer noopener nofollow">[email protected]</a>');

然后 Jest 测试就可以工作了。

有没有办法在不改变我的 Expo 项目的情况下使用 Jest 来测试它?

最佳答案

Configuring the Jest in package.json can solve the problem. Adding the moduleNameMapper will turn your test fine:

"jest": {
    "preset": "jest-expo",
    "moduleNameMapper": {
        "^[@./a-zA-Z0-9$_-]+\\.(png|jpg|gif)$": "RelativeImageStub"
      }
}

关于image - Expo Jest 找不到类似 'file@2x.png' 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51288322/

相关文章:

swift - 去除图像的蓝色和绿色

http - 根据 Angular 2中先前请求的结果执行http请求

unit-testing - 规则引擎和自动化测试难题

asp.net - 访问服务器上使用 WebClient.UploadData 发送的确切数据

c++ - 如何发送原始 JSON Post 请求 C++

ios - iOS 项目的单元测试

javascript - 无法使用 angular 2 指令将带有 Selenium 的文本输入到字段中

image - ffmpeg 将播放图像添加到缩略图输出

html - CSS 图像未正确对齐

javascript - 使用 Canvas 下载图像会增加文件的大小