如您所知,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 themoduleNameMapper
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/