#How to test this function using Jest

4 messages · Page 1 of 1 (latest)

sharp apex
#

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!

pine junco
# sharp apex I have the following function which aims to resize a requested image to the desi...

processImage()

  • Returns image if already cached:
    • Ensure the returned image is the same as expected.
    • Ensure the number of cacheHits is 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 cacheMisses is incremented.
    • Ensure the imagesResizedCount is increased.
    • Ensure the returned image is in the correct format.
  • Resizes the image if a resolution is 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.