Learn how you can mock fetch calls in unit tests with jest and no other library

Photo by Testalize.me on Unsplash

When you write unit tests, you don’t want to make the actual api calls to the server every time you run them. There are a few reasons for that:

  • It’s a unit test, not an integration one.
  • The api owners, even if it’s you, may not appreciate you hitting the api every time the ci runs.
  • You can not test for every possible api response.
  • You can not test for api failures.
  • Much slower tests.
  • and so on…

Check out the following code

We have a function calling an…

Fernando Ferreira

Random things related to Javascript and Typescript and whatever else shows up

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store