Skip to content
Advertisement

Jest: Failed prop type: Invalid prop `source` supplied to `Image`, expected one of type [number]

I am using jest with react-native to run unit test cases. And whenever it encounters Image it throws the following warning

JavaScript

I tried to replace the code to

JavaScript

as suggested in https://stackoverflow.com/a/36460928/3855179 but it started throwing the following warning and the application breaks

JavaScript
JavaScript

Any idea how this can be handled?

Advertisement

Answer

It would be helpful to see your file structure along with the jest configuration file.

But my best guess is – jest isn’t able to find the file in the designated location.

Few things that can be tried:

(1) try changing file location to be a bit more explicit like so:

eg – if image in same directory — source={require('./images/cloud.png')}

eg – if one directory up — source={require('../images/cloud.png')}


(2) try mocking out the image in test file like so:

JavaScript

(3) Specify a location in jest configuration file (or in package.json file if you have a jest key there) for all image types, this way jest will use that as the image source whenever it encounters an image:

eg –

JavaScript

More doco on moduleNameWrapper here – https://jestjs.io/docs/configuration#modulenamemapper-objectstring-string–arraystring

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement