I have the following function which aims to resize a requested image to the desired resolution. If the image was already resized and it's cached, it is sent directly from the redis cache. Example scenario: GET request to http://www.resize.com/image1.jpg?resolution=1920x1080 . If it was not cached, image1.jpg will be resized to 1920x1080 and sent back. I am struggling to make an appropriate jest test and also mock sharp and redis client. Thank you!
#How to test this function using Jest
4 messages · Page 1 of 1 (latest)
processImage()
- Returns image if already cached:
- Ensure the returned image is the same as expected.
- Ensure the number of
cacheHitsis incremented. - Ensure the returned image is in the correct format.
- Loads and processes the image:
- Ensure the image has been cached under
cacheKey. - Ensure the number of
cacheMissesis incremented. - Ensure the
imagesResizedCountis increased. - Ensure the returned image is in the correct format.
- Ensure the image has been cached under
- Resizes the image if a
resolutionis passed before returning:- Ensure the returned image is of the correct size.
--
Your function could be broken up into 2-3 smaller functions, you'd find it easier and more approachable to test them.