Search code examples
node.jsqnock

What is the best way to mock q-io/http requests?


I'm trying to write unit tests for my HTTP service. My service interacts with another remote HTTP service, and I'm using using q-io/http for that interaction.

I would like to use something like the nock package to mock my calls to the remote service, but q-io/http does not seem to be compatible with nock (I'm assuming that this means that the request module is not actually used under the covers of q-io/http as I'd hoped).

Are there any other approaches to mocking q-io/http requests? There does not seem to be an http mocking capability included in Q like there is for files.


Solution

  • It turns out that q-io/http does indeed use the standard request module under the covers, and subsequently, it is possible to use nock with the q-io/http module.

    For me, the problem was that nock was not matching my requests, and the exception was getting swallowed up in a catch. Using the nock log(console.log) mechanism made the matching problems obvious:

    nock(documentUrl)
      .delete('/state')
      .reply(204, {})
      .log(console.log);