cypress-api-testing-cursorrules-prompt-file — Cursor rule
Configures Cursor to write Cypress API tests with schema validation. It detects TypeScript in the project and validates responses using cypress-ajv-schema-validator.
.cursor/rules/cypress-api-testing-cursorrules-prompt-file.mdcWhat cypress-api-testing-cursorrules-prompt-file does
The rule directs the assistant to inspect the repository for TypeScript before writing any tests. It looks for tsconfig.json, checks package.json dependencies, and scans cypress/ for .ts or .tsx files. Test code syntax and file extensions adapt to whatever is found.
Every generated test file targets a single API resource with 3 to 5 focused checks inside describe blocks. The output covers success status codes, unauthorized access handling, and response payloads. Test data relies on fixtures or factories. Isolation is mandatory. Tests do not depend on prior server state.
Response structure verification relies entirely on cypress-ajv-schema-validator. Generated specs will fail immediately unless this library is installed in your project dependencies.
What it helps with
- Generate Cypress API tests with schema validation using
cypress-ajv-schema-validator - Detect TypeScript in the project and set matching file extensions
- Cover error handling and 401 unauthorized status codes for endpoints
- Structure endpoint suites into 3-5 independent tests per resource
Example prompts for the agent
- Write Cypress API tests for
GET /api/users: expect an array with id and name, plus a 401 response for invalid tokens - Create test cases for
POST /api/orders: verify status 201 on success and error payload schema when required fields are missing - Generate Cypress tests for
GET /api/products/1validating response status and JSON schema
How to install cypress-api-testing-cursorrules-prompt-file
Manually: put the files in place
.cursor/rules/cypress-api-testing-cursorrules-prompt-file.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.claude/rules/cypress-api-testing-cursorrules-prompt-file.mdThe paths frontmatter field limits the files: move globs from the source into paths
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.github/instructions/cypress-api-testing-cursorrules-prompt-file.instructions.mdThe applyTo field sets which files it applies to: move globs from the source into applyTo
Manually: put the files in place
.devin/rules/cypress-api-testing-cursorrules-prompt-file.mdManually: put the files in place
.clinerules/cypress-api-testing-cursorrules-prompt-file.mdStep by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules
Questions about cypress-api-testing-cursorrules-prompt-file
How do I install cypress-api-testing-cursorrules-prompt-file in Cursor?
Place the rule file at .cursor/rules/cypress-api-testing-cursorrules-prompt-file.mdc in your project root.
Which library is required for JSON schema validation?
Generated tests rely on cypress-ajv-schema-validator. You must add this package to your project to run validateSchema assertions.
How does the rule handle TypeScript projects?
The assistant looks for tsconfig.json, TypeScript entries in package.json, and .ts or .tsx files inside cypress/. It chooses .ts or .js file extensions and matching syntax based on what it finds.
Source text
cypress-api-testing-cursorrules-prompt-file.mdc · CC0-1.0
---
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);
});
});
});
```
© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules