Search code examples
javascriptnode.jsnock

How to await until nock is called


I'm not sure if nock is supporting awaiting or notifying using callback / event-emitter when the interceptor is called.

For example:

const scope = nock(config.research.url)
                   .post(config.research.routes.getOdds, expectBody)
                   .reply(200)

while(true){
  expect(scope.isDone()).toBeTruthy()
  await sleep(500)
}

My Question

How can I improve the above code using nock API?


Solution

  • nock emits event on request and response: https://github.com/nock/nock/#events

    emit('request', function(req, interceptor, body))
    emit('replied', function(req, interceptor))
    

    Your code can listen to the replied event to get notified.