jest错误:失败的属性类型:提供给`Image`的prop `source`无效

ig9co6j1  于 2023-04-27  发布在  Jest
关注(0)|答案(1)|浏览(186)

运行jest测试时出现此错误。尝试了很多禁用prop types规则:

Warning: Failed prop type: Invalid prop `source` supplied to `Image`, expected one of type [number].
        in Image (at Welcome.js:134)
        in Welcome (created by Connect(Welcome))
        in Connect(Welcome) (at Welcome.test.js:25)
        in Provider (at Welcome.test.js:24)

      132 |             {!global.isFirstTime &&
      133 |                 <TouchableOpacity style={styles.backIcon} onPress={() => props.navigation.goBack()}>
    > 134 |                     <Image source={Images.back} />
          |                    ^
      135 |                 </TouchableOpacity>
      136 |             }
      137 |

已在.eslintrc.json中添加规则

"rules": {
    "react/prop-types": 0
}

同时添加jest配置模块NameMapper

moduleNameMapper: {
        '\\.(css|less)$': 'identity-obj-proxy',
        '^(.+)\\.(jpg|jpeg|gif|png|mp4|mkv|avi|webm|swf|wav|mid)$': 'jest-static-stubs/$2'
    },

因此,当测试运行时,Images.back包含:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg==
1.“react”:“16.13.1”,
1.“react-native”:“0.63.5”,
1.“eslint”:“^8.39.0”,
1.“jest”:“26.6.3”,
谢谢。

pcww981p

pcww981p1#

请尝试:

<Image source={{uri: Images.back}} style={{height:50, width:50}} />

相关问题