【问题标题】:Expo Jest can't find images like 'file@2x.png'Expo Jest 找不到像“file@2x.png”这样的图像
【发布时间】:2018-12-19 15:54:10
【问题描述】:

如您所知,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 对其进行测试?

【问题讨论】:

    标签: image request automated-tests jestjs expo


    【解决方案1】:

    package.json中配置Jest可以解决问题。添加moduleNameMapper 将使您的测试正常:

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

    【讨论】:

      猜你喜欢
      • 2022-09-28
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      相关资源
      最近更新 更多