How to see if nock is matching the request or not? Is there a way one can log information on console regarding nock is matching or not to the requests being made?
Asked
Active
Viewed 1.2k times
4 Answers
18
The log
function is no more available on newer versions of nock e.g. v13.0.0 but you can set the DEBUG
environment variable to nock.*
as described here https://github.com/nock/nock#debugging to log each step of the matching process. For example:
export DEBUG=nock.* && yarn test
if you're using yarn to run your tests.

Christof Aenderl
- 4,233
- 3
- 37
- 48
-
3I would recommend the following single command if you plan on going back and forth from showing debug output to hiding it: `DEBUG=nock.* yarn test` (or `npm test` or whatever). That way the debug output goes away if you remove the first part. – J.M. Janzen Feb 24 '21 at 21:25
9
It is very simple.
Just add .log(console.log)
to your nock object!
nock('https://github.com')
.log(console.log)
.get('/')
Basically, nock checks all the interceptors it has active until a match is found for your request (in case you have multiple nock interceptors mocking a variety of requests). So what will be logged using .log(console.log)
is,
- a whole url to which the request is being made.
- a url from the interceptor
- true or false, depending upon the match scenario. Hope this will help you resolve mysteries of nock :)
-
1I'm getting `TypeError: (0 , _nock.default)(...).get(...).log is not a function` when I try the above code. – Edward D'Souza Mar 11 '21 at 20:26
-
1
You can also listen to nock's event emitters to create a callback for whenever a request is intercepted. For example:
const requestCallback = () => {
console.log('Request was called');
}
nock('https://example.org')
.persist()
.get('/')
.on('request', requestCallback);

mikebridge
- 4,209
- 2
- 40
- 50