# Mocking API Requests in Node tests

DevFeed: [Mocking API Requests in Node tests](<https://devfeed.tech/articles/mocking-api-requests-in-node-tests-52519.md>)

Original publisher: [Read original article](<http://www.jackfranklin.co.uk/blog/mocking-web-requests/>)

Published: 2013-08-19T00:00:00Z

Content type: tutorial

Language: en

Sources: [Jack Franklin](<https://devfeed.tech/sources/jack-franklin.md>)

Topics: [mock api](<https://devfeed.tech/topics/mock-api.md>), [Test-driven development](<https://devfeed.tech/topics/tdd.md>), [Test automation](<https://devfeed.tech/topics/test-automation.md>), [API](<https://devfeed.tech/topics/api.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [mocking](<https://devfeed.tech/tags/mocking.md>), [node](<https://devfeed.tech/tags/node.md>), [tests](<https://devfeed.tech/tags/tests.md>)

## AI overview

Nock lets Node tests mock HTTP requests without changing the code that makes them, avoiding the real network calls that made the author's tests slow, unreliable, and unavailable offline. The example intercepts a GET request and returns a chosen status and body; mocks are consumed after one matching request unless `.persist()` is used. Use `isDone()` to check whether a mocked request was called, and `nock.cleanAll()` after each test to remove mocks. The excerpt ends partway through the discussion of assertions.

## Source excerpt

Recently I sat down with my Pulldown Project, aiming to rewrite the tests. The problem with them was that they were network dependent. Each test would hit the real API and download the real file. This was not good for a number of reasons: I couldn't run the tests without an internet connection the tests were slow the tests were unreliable, they would sometimes pass, and other times not Unreliable tests are worse than no tests, so I ripped them out and started again. Meet Nock The solution to this is Nock, a Node module for mocking HTTP requests. With Nock you can mock a HTTP request and make it always return a specific result. Here's an example: var nock = require("nock"); var http = require("http"); var api = nock("http://javascriptplayground.com") .get("/test/") .reply(200, "Hello World"); http.get("http://javascriptplayground.com/test/", function(resp) { var str = ""; resp.on("data", function(data) { str += data; }); resp.on("end", function() { console.log("Got Result: ", str); }); }); In that code we do two things. First, we mock a request to http://javascriptplayground.com/test/ and make it return the string "Hello World" with a 200 status code. Then we use Node's http library to make a request and log it out. We then get "Got Result: Hello World" outputted when we run the above. What's so great about this is that http.get is none-the-wiser about what just happened. You don't have to change any code to make this work, just mock the request. There's no requirement to return a string, either. You can return an object, an array, whatever you'd like. A Gotcha When you mock something using nock, it only works once. Once a URL you've mocked is hit, the mock is then destroyed. To fix this, you can make a specific mocked URL persist: var api = nock("http://javascriptplayground.com") .persist() .get("/test/") .reply(200, "Hello World"); Now it will last forever, until you call cleanUp, which I'll cover shortly. Asserting If you need to test thaat a specific URL is calle