Search code examples
nock

Nock - how to log the status of each request?


I would like to be able to see each request that an interceptor has intercepted and see whether it has responded or is pending. I am using scope.persist(true) for each interceptor. How can this be done?


Solution

  • Scopes emit events when an Interceptor is matched to a request and when that Interceptor replies with a payload. https://github.com/nock/nock#events

    The callback for each of those events gets passed the Interceptor as an arg.

    I'm not entirely sure what the ask is around "see whether it has responded or is pending", but something like this should get you going:

    const scope = nock('http://example.test')
      .get('/')
      .reply(200)
    
    scope.on('request', (req, interceptor) => {
      console.log('interceptor matched request', interceptor.uri)
    });
    scope.on('replied', (req, interceptor) => {
      console.log('response replied with nocked payload', interceptor.uri)
    });