https://github.com/florpor/puppeteer-mock
Build a bridge between Puppeteer and your favourite HTTP mocking library
https://github.com/florpor/puppeteer-mock
http https mock mocking puppeteer test testing
Last synced: 5 months ago
JSON representation
Build a bridge between Puppeteer and your favourite HTTP mocking library
- Host: GitHub
- URL: https://github.com/florpor/puppeteer-mock
- Owner: florpor
- License: mit
- Created: 2021-02-02T07:57:08.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-02-03T08:30:26.000Z (over 5 years ago)
- Last Synced: 2025-08-09T13:43:06.501Z (12 months ago)
- Topics: http, https, mock, mocking, puppeteer, test, testing
- Language: JavaScript
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# puppeteer-mock
[][npmjs]
[npmjs]: https://www.npmjs.com/package/puppeteer-mock
Build a bridge between Puppeteer and your favourite HTTP mocking library
puppeteer-mock makes the testing of Puppeteer-backed code easy by enabling the mocking of page responses which arrive at the headless browser.
puppeteer-mock should work with any library which mocks Node.js's http/https module responses and is not dependent on a specific mocking library.
## Installation
```bash
$ npm install --save-dev puppeteer-mock
```
## Usage
You can enable puppeteer-mock simply by calling its activate function:
```js
const puppeteerMock = require('puppeteer-mock');
puppeteerMock.activate();
```
**NOTE:** puppeteer-mock does not do the actual HTTP response mocking. You will need to use some library such as [nock](https://github.com/nock/nock) for that. What puppeteer-mock does is the routing of Puppeteer requests through Node.js's http/https modules so that the responses can be mocked using any supported HTTP mocking library.
A recommended way to use puppeteer-mock is to activate it in your tests' setup hook and deactivate it in your teardown hook:
```js
const puppeteerMock = require('puppeteer-mock');
describe('test suite', function() {
beforeEach(function () {
// activate puppeteer-mock
if (!puppeteerMock.isActive())
puppeteerMock.activate();
});
afterEach(function() {
// restore puppeteer requests
puppeteerMock.deactivate();
});
it('some test', function() {
// test code goes here
});
});
```
## API
**puppeteerMock.activate()**
Activate puppeteer-mock to route Puppeteer requests through the library.
**puppeteerMock.deactivate()**
Deactivate puppeteer-mock and have Puppeteer requests routed normally.
**puppeteerMock.isActive()**
Check if puppeteer-mock is currently active.