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.

4
  • 1
    This question is not about using callbacks on async functions. This problem is very specific to reply method in nock module. Commented Sep 5, 2014 at 15:50
  • It doesn't seems possible to do so with nock apparently Commented Mar 13, 2015 at 16:15
  • Related: stackoverflow.com/questions/25684307/… Commented Mar 13, 2015 at 16:42
  • It is not possible at the moment, I have created this bug to handle that case: github.com/pgte/nock/issues/283 Commented Mar 13, 2015 at 16:45

1 Answer 1

-1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Would you open an issue?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.