
Product
Introducing Custom Pull Request Alert Comment Headers
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
pptr-mock-server
Advanced tools
Mocking server responses using Puppeteer.
This library allows to define mock backend responses when testing web app with Puppeteer.
Internally it works purely via Puppeteer API using built-in
setRequestInterception
mechanism. It doesn't set up any servers and doesn't
modify any window APIs like XMLHttpRequest
. This provides great flexibility
and performance when handling requests, since it operates on browser internal
level.
Related reading: Automated UI Testing at Dock.
npm install pptr-mock-server
import puppeteer from 'puppeteer'
import mockServer from 'pptr-mock-server'
// typically your global test setup
const browser = await puppeteer.launch()
const page = await browser.newPage()
const baseAppUrl = 'http://localhost'
const mockRequest = await mockServer.init(page, {
baseAppUrl,
baseApiUrl: baseAppUrl + '/api/'
})
Once you have an instance of a MockRequest you can pass it to your tests for registering mock responses:
const responseConfig = { body: { result: 'ok' } }
mockRequest.on('get', 'http://localhost/api/account', 200, responseConfig)
But since you provided baseApiUrl
as http://localhost/api, you can use relative endpoint name. Also you can use .get()
shorthand method instead of .on()
:
const responseConfig = { body: { result: 'ok' } }
mockRequest.get('account', 200, responseConfig)
When your app performs request to the specified resource, it will respond with the mock response provided.
Handle request to relative endpoint using .on
method:
const responseConfig = { body: { result: 'ok' } }
mockRequest.on('get', 'account', 200, responseConfig)
Using shortcut .get
method and absolute url:
const responseConfig = { body: { result: 'not found' } }
mockRequest.get('https://example.com/test', 404, responseConfig)
Simulate request timeout:
mockRequest.post('search', null, { abort: 'timedout', delay: 10000 })
Once you setup a mock request handler, every matching request will be responded with it. However it's a common scenario when you need to mock a sequence of requests, when over time the same request produces different results. Recommended way to do it is to replace previously registered mock response using new one:
const responseConfig = { body: { result: 'ok' } }
mockRequest.get('account', 200, responseConfig) // returns 200 on each request
// test deleting account logic here
// after account is deleted we want to return 401 instead of 200
mockRequest.get('account', 401) // replaces existing handler
FAQs
Tiny library for backendless testing using Puppeteer
The npm package pptr-mock-server receives a total of 151 weekly downloads. As such, pptr-mock-server popularity was classified as not popular.
We found that pptr-mock-server demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.