I am using superagent for api calls and nock is not intercepting calls when I pass JSON on reply. If I dont pass anything but just response code it works fine.
----Unit test----
import nock from 'nock'
nock('http://somehost:80').log(console.log)
.defaultReplyHeaders({'content-type': 'application/json'})
.get('/location').query(true)
.reply(404);
---- middleware-----
import request from 'superagent';
request.get('http://somehost:80/location')
.query(action.searchParams)
.end((err, res) => {
console.log("response ", res)
console.log("error ", err)
});
When I run above test case it logs response and err. But when I change nock as :
nock('http://somehost:80').log(console.log)
.defaultReplyHeaders({'content-type': 'application/json'})
.get('/location').query(true)
.reply(200,{hello:'hello!!!!'});
There is nothing logged in the console. Even if I just pass String as response it wont work.
But with or without response in the nock, the interceptor is returning true. console.log node_modules\nock\lib\interceptor.js:318 matching http://somehost:80?sid=01&sid=02 to GET http://somehost:80/location with query(true): true
My best guess is its a problem with headers???
node -v : v4.3.1
"jest": "^21.2.1", "nock": "^9.0.21", "superagent": "3.5.2"