Mocking External HTTP Requests in Node Tests with Nock


1 min read

Testing code that depends on external services and APIs, e.g. the GitHub API, has a variety of challenges. Writing tests that make real HTTP requests to these services may be error-prone due to issues such as network connectivity, API changes, rate limiting and so on. Nock allows us to avoid these challenges by intercepting external HTTP requests and enabling us to return custom responses, making unit testing easier.

By the end of this article, you will:

  • Learn how to run unit tests against a real HTTP endpoint
  • Learn how to return different responses to different HTTP endpoints, and
  • Know how to record and playback live HTTP requests to make testing easier.

Read the rest of the article on the Semaphore Blog.