Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cypress-intercept-formdata

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cypress-intercept-formdata

cypress command to work with intreception's multipart/form-data requests

  • 0.5.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
27K
decreased by-8.56%
Maintainers
1
Weekly downloads
 
Created
Source

cypress-intercept-formdata (CIFD)

MIT License npm version circleci status codecov status

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

#Yarn: 
   $ yarn 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");
	});

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");
	});

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:

yarn serve

In terminal 2:

yarn cy:run

OR

yarn cy:open

Testing while developing

In terminal 3:

yarn watch

Keywords

FAQs

Package last updated on 05 Dec 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc