pooja t
pooja t

Reputation: 237

How to see if nock is matching the request or not?

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?

Upvotes: 14

Views: 17051

Answers (4)

mikebridge
mikebridge

Reputation: 4575

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);

Upvotes: 1

John Doherty
John Doherty

Reputation: 4105

You can log requests to the console using:

nock.recorder.rec();

Upvotes: 4

pooja t
pooja t

Reputation: 237

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,

  1. a whole url to which the request is being made.
  2. a url from the interceptor
  3. true or false, depending upon the match scenario. Hope this will help you resolve mysteries of nock :)

Upvotes: 8

Christof Aenderl
Christof Aenderl

Reputation: 4512

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.

Upvotes: 19

Related Questions