Registry / testing / cypress-plugin-api

cypress-plugin-api

JSON →
library2.11.2jsnpmunverified

Cypress Plugin API is a tool for enhancing API testing within the Cypress test runner by providing a user interface similar to Postman. It extends the standard `cy.request()` command with `cy.api()`, which visually logs details about API calls, including URLs, headers, request/response bodies, and cookies, directly into the Cypress App UI. These details are viewable through time-travel snapshots. Key features include JSON data object/array folding, HTTP method color coding, response size calculation, and options for sensitive data masking (like credentials) and integrating API calls seamlessly into UI test flows using `snapshotOnly` mode. The plugin currently maintains version `2.11.2`, released on July 14, 2024, with a consistent cadence of bug fixes and minor feature additions. It ships with TypeScript types to provide a better development experience.

npm install cypress-plugin-api
INSTALL
IMPORT
SIG · CYPRESS-PLUGIN-API
C
cypress-plugin-api
testingjavascriptv2.11.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

*
import 'cypress-plugin-api'
import { api } from 'cypress-plugin-api'
The plugin augments the Cypress `cy` object with `cy.api()`. This import is for its side effects and does not export named symbols directly for `cy.api`.
*
require('cypress-plugin-api')
const api = require('cypress-plugin-api')
CommonJS equivalent for importing the plugin. Like ESM, it's primarily for side effects to extend the `cy` object.
Cypress global types
/// <reference types="cypress-plugin-api" />
While `import 'cypress-plugin-api'` typically handles type augmentation in modern TypeScript setups, explicit reference directives or ensuring `tsconfig.json` includes `cypress-plugin-api` in `types` or `compilerOptions.typeRoots` might be necessary in specific configurations to get IntelliSense for `cy.api()`.

Demonstrates setting up `cypress-plugin-api` in `cypress.config.ts`, importing it, and using `cy.api()` for GET and POST requests. It also showcases `hideCredentials` and `snapshotOnly` environmental options.

import { defineConfig } from 'cypress' // cypress.config.ts export default defineConfig({ e2e: { setupNodeEvents(on, config) { // Optional: Add an event listener to log something during setup on('task', { log(message) { console.log(message) return null }, }) return config }, baseUrl: 'http://localhost:3000', // Ensure your API is running here env: { snapshotOnly: false, // Default: shows full UI for API calls hideCredentials: true, myAuthToken: 'secret-token-123', apiUser: 'testuser', apiPass: 'testpassword', } }, }) // cypress/support/e2e.ts import 'cypress-plugin-api' // You can also add other Cypress commands or configurations here // cypress/e2e/api.cy.ts describe('Cypress Plugin API features', () => { beforeEach(() => { // Assuming your app serves simple endpoints for demonstration // For a real app, ensure your dev server is running before tests }) it('should make a GET API call and display full UI details', () => { cy.api('/data').then((response) => { expect(response.status).to.eq(200) expect(response.body).to.have.property('message', 'Hello from API') }) }) it('should make a POST API call with hidden credentials and custom options', { env: { hideCredentials: true, hideCredentialsOptions: { headers: ['authorization'], body: ['password'], query: ['apiKey'] } } }, () => { cy.api({ method: 'POST', url: '/login', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${Cypress.env('myAuthToken')}` // This header will be hidden }, body: { username: Cypress.env('apiUser'), password: Cypress.env('apiPass') // This body field will be hidden }, qs: { apiKey: 'super-secret-key' // This query param will be hidden } }).then((response) => { expect(response.status).to.eq(200) expect(response.body).to.have.property('success', true) }) }) it('should combine UI and API testing with snapshotOnly mode', { env: { snapshotOnly: true } }, () => { cy.visit('/') // Navigate to your web application cy.get('h1').should('contain', 'Welcome') // Interact with UI cy.api('/users').then((response) => { expect(response.status).to.eq(200) expect(response.body).to.be.an('array') }) cy.get('#user-list').should('exist') // More UI interaction after API call }) })
Debug
Known issues
breakingThis plugin requires Cypress version 10 or newer. Installing with older Cypress versions will result in command `cy.api()` not being available or unexpected behavior.
fix
Upgrade your Cypress installation to version 10 or higher (e.g., `npm install cypress@latest`).
affects: <10.0.0
gotchaThe `cy.api()` command is added globally by importing the plugin in `cypress/support/e2e.{js,ts}`. Failing to import it will result in `cy.api is not a function` errors.
fix
Ensure `import 'cypress-plugin-api'` (ESM) or `require('cypress-plugin-api')` (CommonJS) is present in your support file (e.g., `cypress/support/e2e.ts`).
affects: >=2.0.0
gotchaThe `requestMode` option, which applies `cy.api()`'s UI features to `cy.request()`, is `false` by default. If you intend for all `cy.request()` calls to display in the plugin's UI, you must explicitly enable this option in your Cypress configuration.
fix
Set `env: { requestMode: true }` in your `cypress.config.{js,ts}` file or per-test configuration.
affects: >=2.0.0
gotchaWhen using both `hideCredentials: true` and `hideCredentialsOptions`, the `hideCredentialsOptions` will override the default sensitive fields hidden by `hideCredentials`. Ensure all desired fields are explicitly listed in `hideCredentialsOptions` if you are using it.
fix
Carefully define all fields you wish to hide in the `hideCredentialsOptions` array if `hideCredentials: true` is also enabled, as it will take precedence over defaults.
affects: >=2.0.0
gotchaIncorrect type imports or `tsconfig.json` configuration can lead to TypeScript errors for `cy.api()` or `Cypress.env()` properties, despite the plugin shipping with types. Recent releases have specifically addressed type-related bugs.
fix
Ensure your `tsconfig.json` properly includes the plugin's types (e.g., in `compilerOptions.types` or by including `cypress-plugin-api` in `typeRoots`). If using explicit reference directives, ensure `/// <reference types="cypress-plugin-api" />` is present.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: cy.api is not a function
The `cypress-plugin-api` plugin has not been correctly imported into your Cypress support file.
fix
Add `import 'cypress-plugin-api'` to your `cypress/support/e2e.ts` (or `.js`) file.
Error: Webpack compilation failed: Cannot find module 'cypress-plugin-api'
The `cypress-plugin-api` package is either not installed or webpack cannot resolve its path.
fix
Ensure the package is installed via `npm install cypress-plugin-api` or `yarn add cypress-plugin-api`. Check your `cypress/support/e2e.{js,ts}` path and `tsconfig.json` if applicable.
Property 'api' does not exist on type 'Chainable<any>'
TypeScript is not correctly picking up the type augmentations provided by `cypress-plugin-api` for the `cy` object.
fix
Verify that `cypress-plugin-api` is listed in the `types` array of your `tsconfig.json` (e.g., `"types": ["cypress", "cypress-plugin-api"]`) or that `/// <reference types="cypress-plugin-api" />` is in a relevant `.d.ts` file or `cypress/support/e2e.ts`.
Upgrade
Version history
2.11.2latest on npm
Audit
Dependencies
cypressrequiredPeer dependency, required for the plugin to function within the Cypress test runner.
Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources