Expo Jest 找不到像 'file@2x.png' 这样的图片
Expo Jest can't find images like 'file@2x.png'
如您所知,Expo 通过分析文件的后缀来使用自适应图像大小,如下所示:
xxx@3x.png
xxx@2x.png
xxx@1x.png
我可以这样声明省略文件末尾的图像:
const iconHomeButton = require('./images/homeButton.png');
文件夹中实际的真实图片名称是:
homeButton@2x.png
在 Expo 上它工作正常但是当我尝试使用 Jest 测试它时它找不到图像。
如果我将文件名更改为真实名称,例如:
const iconHomeButton = require('./images/homeButton@2x.png');
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"
}
}
如您所知,Expo 通过分析文件的后缀来使用自适应图像大小,如下所示:
xxx@3x.png
xxx@2x.png
xxx@1x.png
我可以这样声明省略文件末尾的图像:
const iconHomeButton = require('./images/homeButton.png');
文件夹中实际的真实图片名称是:
homeButton@2x.png
在 Expo 上它工作正常但是当我尝试使用 Jest 测试它时它找不到图像。 如果我将文件名更改为真实名称,例如:
const iconHomeButton = require('./images/homeButton@2x.png');
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"
}
}