8

I am trying to test if my API routes are working using nock.js to mock url requests.

My routing file routes according to the following logic:

app.get('/api/study/load/:id', abc.loadStudy );

'loadStudy' method in 'abc.js' is used to handle the below particular GET request. Hence, any GET request from a browser has a 'params' key with 'id' parameter that replaces ':id' in the URL. However, when I try to mock this GET request using nock.js I'm not able to pass this 'id' parameter in the request.

var abc = require('G:\\project\\abc.js');
var nock = require('nock');

var api = nock("http://localhost:3002")
          .get("/api/test/load/1")
          .reply(200, abc.loadStudy);

request({ url : 'http://localhost:3002/api/study/load/1', method: 'GET', params: {id : 1}}, function(error, response, body) { console.log(body);} ); 

My method makes use of 'params' key that is sent with request which I'm not able to mock. Printing 'req' in the below code just gives '/api/test/load/1' . How can I add 'params' to the GET request?

loadStudy = function(req, res) {
    console.log(req);
    var id = req.params.id;
};
Navjot Singh
  • 626
  • 1
  • 5
  • 16

3 Answers3

12

As per the official docs, you can specify querystring by

var api = nock("http://localhost:3002")
      .get("/api/test/load/1")
      .query({params: {id : 1}})
      .reply(200, abc.loadStudy);

Hope it helps.
Revert in case of any doubts.

Sunil Chaudhary
  • 4,481
  • 3
  • 22
  • 41
  • I think this query parameters are the ones that are included as part of the url which is not the case here. I verify this because if I add the query statement, it is not routed to the specified function. My url is complete as 'api/test/load/1', I just want to include 'params' to the request which will have 'id=1' – Navjot Singh Mar 17 '18 at 20:39
  • You can't do this for GET requests. GET request don't have request body. It only have query parameters which can be sent as a part of url. Read here for more details: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET – Sunil Chaudhary Mar 17 '18 at 20:49
  • Please look at the updated question. It's not exactly the request body that I'm looking for but a separate 'params' key which has the 'id' key value pair – Navjot Singh Mar 17 '18 at 21:19
5

I just ran into a similar problem, and tried Sunil's answer. As it turns out, all you have to do is provide a query object you want to match, not an object with params property. I am using nock version 11.7.2

const scope = nock(urls.baseURL)
     .get(routes.someRoute)
     .query({ hello: 'world' })
     .reply(200);
atomic
  • 51
  • 1
  • 2
3

Another way to solve this for a generic url, is to use regex:

// For:
app.get('/api/study/load/:id', handler);

// You can mock:
nock(baseURL).get(/\/api\/study\/load\/[a-zA-Z0-9\-]*/g)

// For a more complex example:
app.get('/api/study/load/:some-guid?param1=val1&param2=val2', handler);

// You can mock:
nock(baseURL).get(/\/api\/study\/load\/[a-zA-Z0-9\-]*\?param1=val1&param2=val2/g)
Mor Shemesh
  • 2,689
  • 1
  • 24
  • 36