14.4.1 • Published 2 years ago

nock-puppeteer v14.4.1

Weekly downloads
477
License
MIT
Repository
github
Last release
2 years ago

nock-puppeteer

Mock http requests with Nock in puppeteer

NPM

Motivation

Integration/E2E tests and scrapers need to mock puppeteer's browser http requests

why not to have the abilities of Nock.js?

"Nock works by overriding Node's http.request function" and when you run tests/scrapers with puppeteer the browser make the http requests

How does it work?

nock-puppeteer simply execute the requests within the test/scraper itself instead of the browser with the help of puppeteer setRequestInterceptor hook.

This package will execute the requests from the node process and will return the mock response (if exist) as a response to the browser

Basic Usage

const puppeteer = require('puppeteer');
const nock = require('nock');
const useNock = require('nock-puppeteer');

(async () => {
 const browser = await puppeteer.launch();
 const page = await browser.newPage();
 useNock(page, ['https://example.com/api'])
 
 const getProducts = await nock('https://example.com/api')
   .get('/api/products')
   .reply(200, [{id: 1, product: 'A'}, {id: 2, product: 'B'}]);

 await page.goto('https://example.com');
 await page.screenshot({path: 'example.png'});
 await browser.close();
})();

API

page - puppeteer page instance

allowedHosts - array of hosts that you want to mock. every request from the browser that include those strings will execute by the node process

14.4.1

2 years ago

1.1.0

2 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago