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

cypress-intercept-formdata

v0.6.0

Published

cypress command to work with Intercept multipart/form-data requests

Downloads

58,153

Readme

cypress-intercept-formdata (CIFD)

This package is intended to be used with Cypress.io intercept command.

As of version 6.2 or 6.3 the request.body accessed from the intercept is an ArrayBuffer for multipart/form-data requests.

This makes it difficult to work with the body of the request and make assertions on it.

CIFD makes it easy to use the multipart body in your specs.

Installation

#pnpm: 
   $ pnpm add cypress-intercept-formdata

#NPM:
   $ npm i cypress-intercept-formdata

Usage

Add to your commands file:


//cypress/support/commands.js

import "cypress-intercept-formdata";

//...

Then in your spec:


cy.intercept("POST", "http://localhost:8888/api/test", {
	statusCode: 200,
	body: { success: true },
}).as("uploadRequest");

//...

cy.wait("@uploadRequest")
	.interceptFormData((formData) => {
		expect(formData["foo"]).to.eq("bar");
	});

Uploading Files

If you have file(s) uploaded as part of the request they will be available in the formData object as well: The value is the file name


cy.wait("@uploadRequest")
	.interceptFormData((formData) => {
		expect(formData["file"]).to.eq("fileName.txt");
	});

Multiple files are also supported:

cy.wait("@uploadRequest")
	.interceptFormData((formData) => {
		expect(formData["file"][0]).to.eq("fileName1.txt");
		expect(formData["file"][1]).to.eq("fileName2.txt");
	});

File Content

By default, CIFD simply adds the file name being uploaded to the formData object. If you'd like to assert more deeply on the file(s) being uploaded, you can set options.loadFileContent to true:

  cy.wait("@submitForm")
    .interceptFormData((formData) => {
        expect(formData["file"]).to.be.instanceof(File);
        expect(formData["file"]).to.have.property("type", "image/jpeg");
        expect(formData["file"].size).to.be.eq(2551829);
    }, { loadFileContent: true });

Multiple files are supported as well.

Use inside intercept routeHandler

Cypress intercept command accepts a routeHandler

If you want to inspect/assert on the body from the handler, you can import the interceptFormData directly and call it like this:

import { interceptFormData } from "cypress-intercept-formdata";

//...

cy.intercept("POST", "http://localhost:8888/api/test", (req) => {
  const formData = interceptFormData(req);
  
  expect(formData["first_name"]).to.eq("James");
});

Testing this Library

In terminal 1:

pnpm serve

In terminal 2:

pnpm cy:run

OR

pnpm cy:open

Testing while developing

In terminal 3:

pnpm watch