npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ea-pptr-mock-server

v1.0.4

Published

Tiny library for backendless testing using Puppeteer

Downloads

10

Readme

NPM downloads CircleCI Coverage Status

pptr-mock-server

Tiny library for backendless testing using Puppeteer.

Intro

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.

NOTE: we use this library heavily in automated tests for Dock and related projects, but api surface is minimalistic, so it may not fit your needs. Contributions are welcome, so we can make this library more flexible and cover more usage scenarios.

Recommended reading: Automated UI Testing at Dock.

Installing

yarn add -D pptr-mock-server

Setting up

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';
this.mockRequest = await mockServer.init(page, {
  baseAppUrl,
  baseApiUrl: baseAppUrl + '/api/'
});

Basic usage

Once you have an instance of MockRequest you can pass it to your tests for registering mock responses:

const responseConfig = {body: {result: 'ok'}};
this.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'}};
this.mockRequest.get('account', 200, responseConfig);

When your app performs request to the specified resource, it will respond with the mock response provided.

Mocking sequence of identical requests

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'}};
this.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
this.mockRequest.get('account', 401); // replaces existing handler

Full api docs