nock – the web service mocking node module

I have recently started to develop a node project where I am promised that all of the information that I will need to render a page will be exposed via API calls. This is fantastic because it significantly simplifies my development as it is easy to call a bunch of services and manipulate data.

The problem is that the services that I am promised to develop my project have not been created yet. Worse yet, the delivery of these services look like it may be weeks from now, but I need to start my development on the front end side of things now!

We’re using node to power our web server, specifically the express web framework and so it seemed only natural to be able to mock our web services within node. Ideally, I should be able to swap my mocked services with the real services in a relatively painless fashion (via a configuration parameter, perhaps?)

flatiron’s offering of nock seems like a brilliant idea. What it does is intercept every HTTP request to a given endpoint and returns your mocked response instead. That, alone, makes nock pretty cool. But wait! There’s more!

nock also provides a way to record any http request and then save the response as a nock mocked object. You just turn on the recorder, make your http request, and then you will have all the code necessary to “nock” it out.

That’s AH-MAZ-ING!