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

playwright-rq

v2.0.1

Published

playwright request mocker

Downloads

13

Readme

playwright-rq

NPM version NPM Downloads

If you are using playwright, and you need mock your network responses, then playwright-rq will help you!

Installing

npm i --save-dev playwright-rq

Usage

Mocker

Allows you to mock for your network responses.

import { Mocker } from 'playwright-rq';
const mocker = new Mocker();
// start mocker with options
await mocker.start({
  page,
  mockList: {
    test: {
      url: 'http://localhost:3000/api',
      method: 'GET',
      response: {
        body: 'test',
        status: 200,
      },
    },
  },
});
// update mocker params
mocker.update({
  test: {
    url: 'http://localhost:3000/api',
    method: 'GET',
    response: {
      body: 'test',
      status: 200,
    },
  },
});

// stop mocker
await mocker.stop();

start options

const options = {
  // playwright page
  page: Page,
  // Indicates where are mocks. Default rootDir = '__remocks__'
  // Absolute path = path.resolve(process.cwd(), rootDir)
  rootDir: 'test/__remocks__',
  // If interceptType does not contain playwright request.resourceType(),
  // then call route.continue()
  // Default interceptType = new Set(['xhr', 'fetch'])
  interceptType: new Set(['xhr', 'image']),
  // If could not find mock for request,
  // and ci = true, then call route.abort().
  // Default ci = false
  ci: true,
  // mockList = Record<string, RequestParams>
  // interface RequestParams {
  //   url: string | RegExp;
  //   method: string;
  //   queryParams?: Record<string, string>;
  //   bodyParams?: Record<string, any>;
  //   resopnse?: {
  //     status?: number;
  //     headers?: Record<string, string>;
  //     body?: string;
  //     filePath?: string;
  //   }
  // }
  // Default response = {
  //   status: 200,
  //   headers: {
  //     'Content-Type': 'application/json; charset=UTF-8',
  //   },
  // }
  mockList: {
    mockName: {
      url: '/api/test',
      method: 'post',
      resopnse: {
        body: '{test:"1234"}',
      },
    },
    test: {
      url: /api\/test/,
      method: 'get',
      queryParams: {
        abst: '1234',
      },
      response: {
        status: 200,
        filePath: 'test',
      },
    },
    mockParams: {
      url: /api\/test/,
      method: 'get',
      bodyParams: {
        abst: '1234',
      },
      response: {
        status: 200,
        headers: {
          'Content-Type': 'text/html; charset=utf-8',
        },
        body: 'text',
      },
    },
  },
};

update options

Updates mockList which you added to mocker.start(). If a mock with such a key already exists, it is updated, otherwise it adds.

mocker.update({
  test: {
    url: 'http://localhost:3000/api',
    method: 'GET',
    response: {
      body: 'test',
      status: 200,
    },
  },
});

RequestListener

Allows you to wait for a request, and return Playwright Request object.

import { RequestListener } from 'playwright-rq';
// start listener for request
const requestListener = new RequestListener({ page, url: 'http://localhost:3000/api' });
await page.click('#button');
// wait request. Dafault timetout = 1000
const request = await requestListener.waitForRequest(3000);
expect(request.postDataJSON()).toEqual({ test: '1234' });

If the request is not found after the specified timeout, then we get an error: Request "${url}" not found, after ${timeout}ms

Debug

playwright-rq uses debug package for logs. Example: DEBUG=playwright-rq* npm run test