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-intercept

v1.0.10

Published

Ergonomically mock, wait for and assert network requests in Playwright

Downloads

30

Readme

playwright-intercept 🎭

This fixture extension provides a Cypress-influenced API (like cy.intercept) for mocking and intercepting network requests in Playwright.

Features

  • Mock and intercept GET, POST, PUT, PATCH and DELETE responses
  • Craft dynamic responses at runtime using data from request params & bodies
  • Assert that requests were made to a specific URL
  • Assert the content of request bodies
  • Wait for a request to be made before continuing
  • Support for named route params (eg. /api/:apiVersion/callback/:id)
  • Strongly typed in TypeScript
  • Test coverage

Installation (npm)

npm i playwright-intercept --save-dev

Setup

Simply extend your base test fixture with the Intercept class, providing optional global configuration options:

  • fixturePathPrefix is the path to your mock data folder, eg. a folder of JSON files containing default response bodies
  • To avoid Playwright log spam, staticExtensions are extensions of files that should never be intercepted
import * as base from "@playwright/test";
import { Intercept } from "playwright-intercept";

const test = base.extend<BaseFixtures>({
  intercept: async ({ page }, use) => {
    await use(
      new Intercept(page, {
        fixturePathPrefix: path.join(process.cwd(), "tests"),
        staticExtensions: ['js', 'css', 'png', 'jpg', 'svg'],
      })
    );
  },
});

Basic Usage

test("Can submit form", async ({ page, intercept }) => {
  // first, set up the intercept, for example:
  const apiFormCallback = intercept.post({
    url: "/api/form/:id",
    statusCode: 200,
    body: {
      status: "success",
    },
    // you can also pass a file for the response body:
    // fixture: "path/to/response-body.json",
    //
    // and modify it on the fly:
    // modifier: ({ body, params }) => {
    //   if (params.id === "foo") {
    //     body.status = "bar";
    //   }
    //   return body;
    // },
    //
    // or pass a handler function for more advanced use cases:
    // handler: ({ route, request, params }) => {
    //   // `route` and `request` are both normal Playwright objects
    //   return route.fulfill({
    //     status: 200,
    //     contentType: 'application/json',
    //     body: `That named route param was ${params.id}`,
    //   });
    // },
  });

  await page.goto("/my-form");

  await page.fill('input[name="name"]', "Bar");

  await page.locator("#submit-button").click();

  await apiFormCallback.wait();

  await expect(apiFormCallback.requests[0].postDataJSON()).toBe({
    name: "Bar",
  });

  apiFormCallback.update({
    statusCode: 400,
    body: {
      status: "fail",
    }
  });

  await page.locator("#submit-button").click();

  await apiFormCallback.wait();

  await page.waitForSelector('div[role="alert"]');
});

[!TIP]
This repo's tests folder contains examples that demonstrate the setup and functionality of playwright-intercept.

Suggested Implementation Pattern

We recommend you create fixtures of Intercept instances, logically grouped together. To see this pattern demonstrated, check out collection-of-intercepts-as-fixture.spec.ts in this repo.

We understand that everybody has their own preferred implementation pattern, so if you've found another great way to structure your Intercept instances in your Playwright codebase, please let us know in a PR or Issue!

API

intercept[.get, .post, .patch, .put, .delete]

type BaseOptions = {
  url: string;
  statusCode?: number;
};

type MimeTypeOption = {
  mimeType?: string;
};

type ModifierOption = {
  modifier?: (args: {
    body: any;
    params: Record<string, string>;
    request: Request;
  }) => any;
};

type BodyOption = {
  body: Buffer | object | string;
};

type FixtureOption = {
  fixture:
    | string
    | ((args: { route: Route; params: Record<string, string> }) => string);
};

type HandlerOption = {
  handler: (args: {
    route: Route;
    params: Record<string, string>;
    request: Request;
  }) => void;
};

export type InterceptOptions = BaseOptions &
  (
    | (MimeTypeOption & ModifierOption & (BodyOption | FixtureOption))
    | HandlerOption
    | { statusCode: number }
  );

At test runtime, InterceptSurface provides some methods and reactive attributes to help you assert requests.

.wait({ timeout?: number })

Resolves promise if a request has been made to the URL via selected method.

await myIntercept.wait({ timeout: 1000 });

.requests

Gives you direct access to all requests made to the URL.

expect(myIntercept.requests[0].postData()).toEqual({
  body: "foo"
});

expect(myIntercept.requests).toHaveLength(2);

.update(InterceptOptions | (InterceptOptions) => InterceptOptions)

Allows you to change the options of an intercept post-initialization for the lifetime of the test spec.

myIntercept.update({
  statusCode: 400,
  body: { id: 1 },
});

Contributing

Contributions are welcome! Please open an issue or PR if you have any suggestions or improvements.