0

I'm using nock module to intercept HTTP calls to my API service (still undeveloped) and return some mock data that I have in a temporary database.

I notice the http requests are intercepted properly, but the problem that I see is that before my function gets required data from temp DB, the nock replies.

For a simple demonstration, look at the code below:

var nock = require('nock');

var nockReq = nock("http://localhost:8000")

    .post("/sample-endpoint")

    .reply(200, function (uri, requestBody) {
        setTimeout(function() {
            return {"result": "TIMED OUT"}
        }, 2000
    );

With the above code, when I don't use a time-out, I get the data returned properly as expected. But with this time-out, the nock doesn't seem to wait for the callback, instead proceed responding an empty 200 response.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ananth
  • 767
  • 1
  • 7
  • 15
  • 1
    This question is not about using callbacks on async functions. This problem is very specific to reply method in nock module. – Ananth Sep 05 '14 at 15:50
  • It doesn't seems possible to do so with nock apparently – Natim Mar 13 '15 at 16:15
  • Related: http://stackoverflow.com/questions/25684307/node-js-module-nock-reply-with-a-callback-doesnt-work – Natim Mar 13 '15 at 16:42
  • It is not possible at the moment, I have created this bug to handle that case: https://github.com/pgte/nock/issues/283 – Natim Mar 13 '15 at 16:45

1 Answers1

-1

You just put the function in setTimeOut to the next tick with nothing returned. Try add return before setTimeOut.

Andrew Carl
  • 802
  • 1
  • 8
  • 15