3

I would like to use nock for mocking HTTP requests in puppeteer but for doing it nock should run in the same node process. Is there any solution or workaround for that?

nock has great functionality that relevant also for e2e tests or scrapers

1 Answers1

1

This is not currently possible.

Unfortunately, running in the same process is only one of the problems. Puppeteer works by controlling a Chrome browser, usually headless. Browsers make outbound HTTP calls via XHR/fetch while Nock works by monkey patching the HTTP lib native to Node. So even if you setup Nock in the same process as the one making the request, Nock would not be able to intercept it.

Matt R. Wilson
  • 7,268
  • 5
  • 32
  • 48
  • 1
    Thanks @mattr.wilson , puppeteer actually has a not that bad way to intercept requests I think what could work is to have a kind of proxy that will do the request instead of the browser. That proxy will use Node native HTTP lib. – Guy Serfaty May 01 '20 at 08:08