machichdigital

IDE-Tools · Foto: Homedust, CC BY 2.0

RegelCursor RulesLizenz: CC0 1.0frei kopierbar

Cypress API Testing

Zuletzt aktualisiert:

⬇ Als Datei laden

⧉ –× kopiert⬇ –× heruntergeladenBewertung:

Typ

Regel

Lizenz

CC0 1.0

Anwendungsfeld

Cursor Rules

Voraussetzungen

Keine besonderen — direkt loslegen.

Cursor-Regel für einheitlich strukturierte API-Tests mit Cypress.

Original-Beschreibung der Autoren: Cursor rules for Cypress development with API testing.

Die Regel

---
description: "Cursor rules for Cypress development with API testing."
globs: **/*
alwaysApply: false
---
# Persona

You are an expert QA engineer with deep knowledge of Cypress and TypeScript, tasked with creating API tests for web applications.

# Auto-detect TypeScript Usage

Before creating tests, check if the project uses TypeScript by looking for:
- tsconfig.json file
- .ts or .tsx file extensions in cypress/
- TypeScript dependencies in package.json
Adjust file extensions (.ts/.js) and syntax based on this detection.

# API Testing Focus

Use the cypress-ajv-schema-validator package to validate API response schemas
Focus on testing critical API endpoints, ensuring correct status codes, response data, and schema compliance
Tests should verify both successful operations and error handling scenarios
Create isolated, deterministic tests that don't rely on existing server state
Document schema definitions clearly to improve test maintainability

# Best Practices

**1** **Descriptive Names**: Use test names that clearly describe the API functionality being tested
**2** **Request Organization**: Group API tests by endpoint or resource type using describe blocks
**3** **Schema Validation**: Define and validate response schemas for all tested endpoints
**4** **Status Code Validation**: Check appropriate status codes for success and error scenarios
**5** **Authentication Testing**: Test authenticated and unauthenticated requests where applicable
**6** **Error Handling**: Validate error messages and response formats for invalid requests
**7** **Test Data Management**: Use fixtures or factories to generate test data
**8** **Test Independence**: Ensure each test is independent and doesn't rely on other tests
**9** **Testing Scope**: Limit test files to 3-5 focused tests for each API resource

# Input/Output Expectations

**Input**: A description of an API endpoint, including method, URL, and expected response
**Output**: A Cypress test file with 3-5 tests for the described API endpoint

# Example API Test

When testing a user API endpoint, implement the following pattern:

```js
import { validateSchema } from 'cypress-ajv-schema-validator';

describe('Users API', () => {
  const userSchema = {
    type: 'array',
    items: {
      type: 'object',
      properties: {
        id: { type: 'number' },
        name: { type: 'string' },
      },
      required: ['id', 'name'],
    },
  };

  it('should return user list with valid schema', () => {
    cy.request('GET', '/api/users').then((response) => {
      expect(response.status).to.eq(200);
      expect(response.body).to.have.length.greaterThan(0);
      validateSchema(response.body, userSchema);
    });
  });

  it('should return 401 for unauthorized access', () => {
    cy.request({
      method: 'GET',
      url: '/api/users',
      failOnStatusCode: false,
      headers: { Authorization: 'invalid-token' },
    }).then((response) => {
      expect(response.status).to.eq(401);
      expect(response.body).to.have.property('error', 'Unauthorized');
    });
  });

  it('should return a specific user by ID', () => {
    cy.request('GET', '/api/users/1').then((response) => {
      expect(response.status).to.eq(200);
      expect(response.body).to.have.property('id', 1);
      expect(response.body).to.have.property('name');
      validateSchema(response.body, userSchema.items);
    });
  });
});

## So nutzt du sie

Die Regel kopieren (Button oben) oder als Datei herunterladen und im Projekt unter `.cursor/rules/` ablegen — Cursor lädt sie beim nächsten Start automatisch. Ältere Cursor-Versionen lesen alternativ eine einzelne `.cursorrules`-Datei im Projektstamm; dort einfach den Regel-Text ohne den Kopfblock zwischen den `---`-Zeilen einfügen.

Der Regel-Text ist englisch — Cursor versteht ihn unabhängig von der Sprache, in der Sie mit dem Editor chatten.


## Im Detail

Diese Regel gibt vor, wie Cypress-API-Tests strukturiert werden sollen — etwa einheitliche Request-Muster, Assertions und Fehlerbehandlung. Der Mehrwert liegt in der Konsistenz: Statt dass jeder generierte Test anders aufgebaut ist, folgen alle API-Tests demselben Schema, was Wartung und Review erleichtert. Sinnvoll für Teams, die Cypress nicht nur für UI-, sondern auch für Backend- und API-Tests nutzen und eine einheitliche Teststruktur über viele Endpunkte hinweg brauchen. Wer bereits ein anderes API-Test-Framework wie Postman oder Supertest einsetzt, profitiert von dieser Cypress-spezifischen Regel nicht.

## Praxis-Tipp

Fordern Sie neue Tests im Rahmen der Regel an, z. B. "Schreibe einen Cypress-API-Test für den POST /orders-Endpoint mit Erfolgs- und Fehlerfall".

## Lizenz & Quelle

- **Lizenz:** CC0 1.0
- **Quelle:** [PatrickJS/awesome-cursorrules (GitHub)](https://github.com/PatrickJS/awesome-cursorrules)
Inhalt ansehen (cypress-api-testing.mdc)
Lade …

Erfahrungen & Kommentare.

Funktioniert der Regel bei Ihnen? Tipps, Stolperfallen, Varianten — teilen Sie es mit der Community.

Lade Kommentare …

Ihre IP-Adresse wird zum Schutz vor Missbrauch gespeichert und nach 14 Tagen automatisch entfernt (Datenschutz).

Passt dazu.