mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+201
-319
@@ -1,345 +1,227 @@
|
||||
---
|
||||
description: This rule provides a comprehensive guide to Cypress best practices, covering code organization, performance, security, testing strategies, and tooling to ensure robust and maintainable end-to-end tests.
|
||||
globs: *.cy.js,*.cy.ts,*.spec.js,*.spec.ts
|
||||
description: This guide defines definitive best practices for writing maintainable, performant, and reliable Cypress end-to-end tests, leveraging modern patterns and avoiding common pitfalls.
|
||||
globs: **/*.{js,ts,cy.js,cy.ts}
|
||||
---
|
||||
# Cypress Best Practices: A Comprehensive Guide
|
||||
# Cypress Best Practices
|
||||
|
||||
This guide provides a comprehensive set of best practices for using Cypress, a popular end-to-end testing framework for web applications. Following these guidelines will help you write more robust, maintainable, and efficient tests.
|
||||
Cypress is our go-to for robust E2E testing. Follow these rules to ensure our test suite remains fast, stable, and easy to maintain.
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
## 1. Code Organization & Structure
|
||||
|
||||
Good code organization is crucial for maintaining large Cypress test suites. Here are recommendations for structuring your project:
|
||||
Adhere to the standard Cypress folder structure. This promotes consistency and discoverability.
|
||||
|
||||
### Directory Structure
|
||||
**Rule:** Keep a single `cypress.config.ts` at the root.
|
||||
**Rule:** Place E2E tests in `cypress/e2e`.
|
||||
**Rule:** Store reusable commands in `cypress/support/commands.ts`.
|
||||
**Rule:** Use `cypress/fixtures` for static test data.
|
||||
|
||||
```typescript
|
||||
// cypress.config.ts
|
||||
import { defineConfig } from 'cypress';
|
||||
|
||||
cypress/
|
||||
├── e2e/ # End-to-end tests
|
||||
│ ├── example.cy.js # Example test file
|
||||
│ └── ...
|
||||
├── fixtures/ # Test data
|
||||
│ ├── example.json # Example fixture file
|
||||
│ └── ...
|
||||
├── support/ # Custom commands and utility functions
|
||||
│ ├── commands.js # Custom Cypress commands
|
||||
│ ├── e2e.js # Runs before each test file
|
||||
│ └── ...
|
||||
├── downloads/ # Directory for downloaded files from tests
|
||||
├── screenshots/ # Directory for screenshots of test failures
|
||||
├── videos/ # Directory for test execution videos
|
||||
cypress.config.js # Cypress configuration file
|
||||
package.json # Node.js package file
|
||||
export default defineConfig({
|
||||
e2e: {
|
||||
baseUrl: 'http://localhost:3000', // Always set a global base URL
|
||||
retries: {
|
||||
runMode: 2, // Allow retries in CI for flaky tests
|
||||
openMode: 0,
|
||||
},
|
||||
setupNodeEvents(on, config) {
|
||||
// Implement cy.task() here for server-side operations
|
||||
},
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
## 2. Test Isolation
|
||||
|
||||
**Explanation:**
|
||||
Each test (`it` block) must be independent and not rely on the state of previous tests. This enables parallelization and makes debugging straightforward.
|
||||
|
||||
* `e2e/`: Contains your end-to-end tests. Organize tests by feature or component.
|
||||
* `fixtures/`: Stores static test data (JSON files). Use fixtures to avoid hardcoding data in your tests.
|
||||
* `support/`: Holds custom commands and utility functions. This promotes code reuse and keeps tests concise.
|
||||
* `downloads/`, `screenshots/`, `videos/`: Cypress automatically manages these directories for test artifacts.
|
||||
* `cypress.config.js`: The main configuration file for Cypress.
|
||||
* `package.json`: Standard Node.js package definition.
|
||||
❌ **BAD:** Dependent tests
|
||||
```typescript
|
||||
// test-suite.cy.ts
|
||||
describe('User Flow', () => {
|
||||
it('should register a user', () => { /* ... */ }); // Creates user
|
||||
it('should log in the registered user', () => { /* ... */ }); // Depends on previous test
|
||||
});
|
||||
```
|
||||
|
||||
### File Naming Conventions
|
||||
✅ **GOOD:** Isolated tests using `beforeEach`
|
||||
```typescript
|
||||
// test-suite.cy.ts
|
||||
describe('User Management', () => {
|
||||
beforeEach(() => {
|
||||
// Programmatically create user and log in via API or custom command
|
||||
cy.apiLogin('testuser@example.com', 'password123');
|
||||
cy.visit('/dashboard');
|
||||
});
|
||||
|
||||
* Test files: `[feature].cy.js` or `[component].spec.js` (e.g., `login.cy.js`, `userProfile.spec.js`).
|
||||
* Fixture files: `[data_description].json` (e.g., `valid_user.json`, `product_details.json`).
|
||||
* Custom command files: `[command_name].js` (if multiple command files are created in support/).
|
||||
it('should display user dashboard', () => {
|
||||
cy.getBySel('dashboard-header').should('contain', 'Welcome, Test User');
|
||||
});
|
||||
|
||||
### Module Organization
|
||||
it('should allow user to update profile', () => {
|
||||
cy.getBySel('profile-link').click();
|
||||
cy.getBySel('name-input').type('New Name');
|
||||
cy.getBySel('save-button').click();
|
||||
cy.getBySel('success-message').should('be.visible');
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
* **Custom Commands:** Place custom commands in `cypress/support/commands.js`. Create separate files for related commands and import them into `commands.js` to improve organization.
|
||||
* **Page Objects:** If using the Page Object Model (see below), create separate files for each page object and store them in a dedicated directory (e.g., `cypress/page_objects/`).
|
||||
* **Utility Functions:** Create utility functions for common tasks (e.g., data generation, API calls) and store them in `cypress/support/utils.js` or a similar named file/directory.
|
||||
## 3. Element Selection
|
||||
|
||||
### Component Architecture
|
||||
Prioritize stable, test-specific attributes. This decouples selectors from styling or content changes.
|
||||
|
||||
While Cypress is primarily for end-to-end testing, thinking in terms of components can help structure your tests:
|
||||
**Rule:** Always use `data-cy` attributes for element selection.
|
||||
**Rule:** Use `cy.contains()` only when the displayed text is critical to the test's purpose.
|
||||
|
||||
* **Page Object Model (POM):** A design pattern where each page or section of the application is represented as a class. The class contains selectors for elements on the page and methods for interacting with those elements. This centralizes element selection and reduces code duplication. **Example:**
|
||||
❌ **BAD:** Brittle selectors
|
||||
```typescript
|
||||
cy.get('.btn-primary').click(); // Coupled to styling
|
||||
cy.get('#submitButton').click(); // Coupled to JS/CSS ID
|
||||
cy.get('div:nth-child(2) > p').should('be.visible'); // Highly brittle DOM structure
|
||||
```
|
||||
|
||||
javascript
|
||||
// cypress/page_objects/loginPage.js
|
||||
class LoginPage {
|
||||
getEmailField() {
|
||||
return cy.get('[data-cy="email"]');
|
||||
}
|
||||
✅ **GOOD:** Resilient selectors with `data-cy`
|
||||
```typescript
|
||||
// In cypress/support/commands.ts
|
||||
Cypress.Commands.add('getBySel', (selector: string, ...args: any[]) => {
|
||||
return cy.get(`[data-cy="${selector}"]`, ...args);
|
||||
});
|
||||
|
||||
getPasswordField() {
|
||||
return cy.get('[data-cy="password"]');
|
||||
}
|
||||
// In your test file
|
||||
cy.getBySel('submit-button').click(); // Best practice
|
||||
cy.getBySel('user-profile-name').should('contain', 'John Doe');
|
||||
cy.contains('Submit Order').click(); // Only if "Submit Order" text is critical
|
||||
```
|
||||
|
||||
getSubmitButton() {
|
||||
return cy.get('[data-cy="submit"]');
|
||||
}
|
||||
## 4. Mocking Strategies
|
||||
|
||||
login(email, password) {
|
||||
this.getEmailField().type(email);
|
||||
this.getPasswordField().type(password);
|
||||
this.getSubmitButton().click();
|
||||
}
|
||||
}
|
||||
Control external dependencies to ensure fast, reliable, and deterministic tests.
|
||||
|
||||
export default new LoginPage();
|
||||
**Rule:** Use `cy.intercept()` for all network requests (XHR/Fetch).
|
||||
**Rule:** Use `cy.fixture()` to provide static response data.
|
||||
**Rule:** Use `cy.stub()` for client-side function mocking.
|
||||
**Rule:** For server-side setup/teardown, use `cy.task()` to execute Node.js code.
|
||||
|
||||
// cypress/e2e/login.cy.js
|
||||
import loginPage from '../page_objects/loginPage';
|
||||
❌ **BAD:** Relying on live API responses
|
||||
```typescript
|
||||
it('should load products', () => {
|
||||
cy.visit('/products');
|
||||
cy.get('.product-card').should('have.length.gt', 0); // Fails if API is down or slow
|
||||
});
|
||||
```
|
||||
|
||||
describe('Login', () => {
|
||||
it('should log in successfully', () => {
|
||||
cy.visit('/login');
|
||||
loginPage.login('test@example.com', 'password');
|
||||
cy.url().should('include', '/dashboard');
|
||||
✅ **GOOD:** Intercepting network requests with fixtures
|
||||
```typescript
|
||||
it('should load products from fixture', () => {
|
||||
cy.intercept('GET', '/api/products', { fixture: 'products.json' }).as('getProducts');
|
||||
cy.visit('/products');
|
||||
cy.wait('@getProducts');
|
||||
cy.getBySel('product-card').should('have.length', 3);
|
||||
});
|
||||
```
|
||||
|
||||
## 5. Avoiding Arbitrary Waits
|
||||
|
||||
Cypress automatically retries assertions. Avoid `cy.wait()` unless explicitly waiting for a network request to complete.
|
||||
|
||||
❌ **BAD:** Arbitrary waits
|
||||
```typescript
|
||||
cy.get('button').click();
|
||||
cy.wait(2000); // Unnecessary, slows down tests, can cause flakiness
|
||||
cy.get('.success-message').should('be.visible');
|
||||
```
|
||||
|
||||
✅ **GOOD:** Leveraging Cypress's retry-ability
|
||||
```typescript
|
||||
cy.getBySel('submit-button').click();
|
||||
cy.getBySel('success-message').should('be.visible'); // Cypress retries until visible
|
||||
```
|
||||
✅ **GOOD:** Waiting for specific network requests
|
||||
```typescript
|
||||
cy.intercept('POST', '/api/users', { statusCode: 201 }).as('createUser');
|
||||
cy.getBySel('register-form').submit();
|
||||
cy.wait('@createUser').its('response.statusCode').should('eq', 201);
|
||||
```
|
||||
|
||||
## 6. Performance & Real-world Scenarios
|
||||
|
||||
Optimize test execution and simulate real user interactions effectively.
|
||||
|
||||
**Rule:** Log in programmatically via `cy.request()` or a custom command in `beforeEach` instead of using the UI. This is significantly faster.
|
||||
**Rule:** Use `cy.session()` for persistent login state across tests within a spec file, reducing redundant login calls.
|
||||
**Rule:** Leverage `cy.prompt()` (available 2025) for natural language test generation and self-healing selectors.
|
||||
|
||||
```typescript
|
||||
// cypress/support/commands.ts
|
||||
Cypress.Commands.add('apiLogin', (email, password) => {
|
||||
cy.session([email, password], () => { // Cache session for faster subsequent runs
|
||||
cy.request('POST', '/api/login', { email, password })
|
||||
.its('body.token')
|
||||
.then((token) => {
|
||||
localStorage.setItem('jwt', token); // Store token for authenticated requests
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
### Code Splitting
|
||||
|
||||
* Cypress doesn't directly support code splitting in the same way as a frontend application. However, you can still organize your code into smaller, manageable files to improve maintainability and readability. Use `require()` or ES module imports (`import/export`) to split your code into modules.
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
|
||||
### Design Patterns
|
||||
|
||||
* **Page Object Model (POM):** See above. Encapsulates page-specific logic.
|
||||
* **Custom Commands:** Create custom commands for frequently used actions. This makes tests more readable and reduces code duplication. **Example:**
|
||||
|
||||
javascript
|
||||
// cypress/support/commands.js
|
||||
Cypress.Commands.add('login', (email, password) => {
|
||||
cy.request('POST', '/api/login', { email, password }).then((response) => {
|
||||
cy.setCookie('auth_token', response.body.token);
|
||||
cy.visit('/dashboard'); // Or however the app navigates
|
||||
});
|
||||
});
|
||||
|
||||
// cypress/e2e/dashboard.cy.js
|
||||
describe('Dashboard', () => {
|
||||
it('should display user information', () => {
|
||||
cy.login('user@example.com', 'password');
|
||||
cy.contains('Welcome, User');
|
||||
});
|
||||
});
|
||||
|
||||
* **Fixtures:** Load test data from fixture files to keep tests data-driven and avoid hardcoding values. **Example:**
|
||||
|
||||
javascript
|
||||
// cypress/fixtures/user.json
|
||||
{
|
||||
"email": "test@example.com",
|
||||
"password": "password"
|
||||
}
|
||||
|
||||
// cypress/e2e/login.cy.js
|
||||
describe('Login', () => {
|
||||
it('should log in with valid credentials', () => {
|
||||
cy.fixture('user').then((user) => {
|
||||
cy.visit('/login');
|
||||
cy.get('[data-cy="email"]').type(user.email);
|
||||
cy.get('[data-cy="password"]').type(user.password);
|
||||
cy.get('[data-cy="submit"]').click();
|
||||
cy.url().should('include', '/dashboard');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
### Recommended Approaches for Common Tasks
|
||||
|
||||
* **Logging in:** Use `cy.request()` to programmatically log in (as shown in the examples above). This is faster and more reliable than logging in through the UI.
|
||||
* **Selecting elements:** Use `data-*` attributes for selecting elements. This makes tests more resilient to changes in CSS or JavaScript.
|
||||
* **Waiting for API calls:** Use `cy.intercept()` to wait for specific API calls before proceeding with the test. Avoid using `cy.wait()` with fixed timeouts.
|
||||
* **Handling asynchronous operations:** Cypress automatically waits for elements to become available, so avoid using manual timeouts.
|
||||
|
||||
### Anti-patterns and Code Smells
|
||||
|
||||
* **Hardcoding data:** Avoid hardcoding data directly in your tests. Use fixtures instead.
|
||||
* **Using brittle selectors:** Avoid using CSS classes or IDs that are likely to change.
|
||||
* **Relying on fixed timeouts:** Avoid using `cy.wait()` with fixed timeouts. Use `cy.intercept()` or Cypress's built-in waiting mechanisms.
|
||||
* **Chaining too many commands:** While command chaining is powerful, excessive chaining can make tests harder to read and debug. Break down complex chains into smaller, more manageable chunks.
|
||||
* **Sharing state between tests:** Each test should be independent and not rely on the state left by other tests. Use `beforeEach()` to set up a clean state for each test.
|
||||
* **Testing implementation details:** Tests should focus on the user's perspective and not test implementation details that are likely to change.
|
||||
* **Ignoring error messages:** Pay attention to error messages and use them to debug your tests.
|
||||
* **Over-reliance on UI for setup:** Where possible, programmatically set up the application state using `cy.request` to seed data or log in users directly rather than navigating through the UI for every test.
|
||||
|
||||
### State Management
|
||||
|
||||
* Cypress tests should be independent and self-contained. Each test should set up its own initial state. Use `beforeEach()` hooks to reset the state before each test.
|
||||
* Use `cy.request()` to seed data or log in users directly to set up the application state.
|
||||
* Avoid sharing state between tests, as this can lead to unreliable and unpredictable results. Use `cy.clearCookies()`, `cy.clearLocalStorage()`, and `cy.clearSessionStorage()` to clear data between tests.
|
||||
|
||||
### Error Handling
|
||||
|
||||
* Cypress provides detailed error messages that can help you debug your tests. Pay attention to these messages and use them to identify the root cause of the problem.
|
||||
* Use `try...catch` blocks to handle errors in custom commands or utility functions.
|
||||
* Use `Cypress.on('uncaught:exception', (err, runnable) => { ... })` to handle uncaught exceptions in the application.
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
### Optimization Techniques
|
||||
|
||||
* **Minimize UI interactions:** Use `cy.request()` to set up the application state instead of interacting with the UI.
|
||||
* **Use `cy.intercept()` for waiting:** Wait for specific API calls instead of using fixed timeouts.
|
||||
* **Run tests in parallel:** Cypress Cloud enables parallel test execution to reduce overall test time.
|
||||
* **Optimize selectors:** Use efficient selectors that quickly locate elements.
|
||||
* **Filter tests using tags:** Use tags to selectively run tests based on feature or component.
|
||||
|
||||
### Memory Management
|
||||
|
||||
* Cypress runs in the browser, so memory management is generally handled by the browser. However, it's important to be aware of potential memory leaks in your application.
|
||||
* Avoid creating large data structures in your tests.
|
||||
* Use `cy.clearCookies()`, `cy.clearLocalStorage()`, and `cy.clearSessionStorage()` to clear data between tests.
|
||||
|
||||
### Rendering Optimization
|
||||
|
||||
* N/A - Cypress doesn't directly handle rendering. Rendering is handled by the application being tested.
|
||||
|
||||
### Bundle Size Optimization
|
||||
|
||||
* The size of your test files can affect the performance of your tests. Keep your test files as small as possible.
|
||||
* Remove unused code from your test files.
|
||||
* Use a bundler (e.g., Webpack, Parcel) to bundle your test files.
|
||||
|
||||
### Lazy Loading
|
||||
|
||||
* N/A - Cypress doesn't directly support lazy loading. Lazy loading is a feature of the application being tested.
|
||||
|
||||
## 4. Security Best Practices
|
||||
|
||||
### Common Vulnerabilities and How to Prevent Them
|
||||
|
||||
* **Cross-Site Scripting (XSS):** Prevent XSS vulnerabilities by properly escaping user input in your application. Cypress can be used to test for XSS vulnerabilities by injecting malicious code into input fields and verifying that it is properly escaped.
|
||||
* **Cross-Site Request Forgery (CSRF):** Prevent CSRF vulnerabilities by using CSRF tokens in your application. Cypress can be used to test for CSRF vulnerabilities by attempting to submit forms without a valid CSRF token.
|
||||
* **SQL Injection:** Prevent SQL injection vulnerabilities by using parameterized queries or an ORM. Cypress can be used to test for SQL injection vulnerabilities by injecting malicious SQL code into input fields and verifying that it is properly escaped.
|
||||
|
||||
### Input Validation
|
||||
|
||||
* Validate all user input on both the client-side and server-side.
|
||||
* Use strong input validation to prevent malicious input from reaching your application.
|
||||
* Cypress can be used to test input validation by providing invalid input and verifying that the application properly handles it.
|
||||
|
||||
### Authentication and Authorization
|
||||
|
||||
* Use a secure authentication and authorization system.
|
||||
* Use strong passwords and enforce password complexity requirements.
|
||||
* Use multi-factor authentication for added security.
|
||||
* Cypress can be used to test authentication and authorization by attempting to access protected resources without proper credentials.
|
||||
|
||||
### Data Protection
|
||||
|
||||
* Protect sensitive data by encrypting it at rest and in transit.
|
||||
* Use HTTPS to encrypt data in transit.
|
||||
* Store sensitive data in a secure location.
|
||||
* Cypress can be used to test data protection by verifying that sensitive data is properly encrypted.
|
||||
|
||||
### Secure API Communication
|
||||
|
||||
* Use HTTPS for all API communication.
|
||||
* Use API keys or other authentication mechanisms to protect your APIs.
|
||||
* Validate all data received from APIs.
|
||||
* Cypress can be used to test API security by attempting to access APIs without proper credentials or by sending invalid data.
|
||||
|
||||
## 5. Testing Approaches
|
||||
|
||||
### Unit Testing
|
||||
|
||||
* While Cypress excels at end-to-end testing, unit testing is still important for testing individual components in isolation. Use a unit testing framework like Mocha or Jest for unit testing.
|
||||
* For components that interact heavily with the UI, consider using Cypress Component Testing.
|
||||
|
||||
### Integration Testing
|
||||
|
||||
* Integration tests verify that different parts of the application work together correctly. Cypress can be used to write integration tests by testing the interactions between different components or services.
|
||||
|
||||
### End-to-End Testing
|
||||
|
||||
* End-to-end tests verify that the entire application works correctly from the user's perspective. Cypress is ideal for writing end-to-end tests.
|
||||
* Focus on testing key user flows and critical functionality.
|
||||
|
||||
### Test Organization
|
||||
|
||||
* Organize your tests by feature or component.
|
||||
* Use descriptive names for your test files and test cases.
|
||||
* Use tags to categorize your tests.
|
||||
* Create a clear and consistent testing strategy.
|
||||
|
||||
### Mocking and Stubbing
|
||||
|
||||
* **Mocking:** Replace a dependency with a mock object that simulates the behavior of the dependency. Use `cy.stub()` to mock functions and `cy.intercept()` to mock API calls.
|
||||
* **Stubbing:** Override the behavior of a function or API call with a predefined response. Use `cy.stub()` to stub functions and `cy.intercept()` to stub API calls.
|
||||
* Use mocking and stubbing to isolate your tests and control the behavior of external dependencies. This makes tests more predictable and reliable.
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
### Frequent Mistakes
|
||||
|
||||
* Using brittle selectors.
|
||||
* Relying on fixed timeouts.
|
||||
* Sharing state between tests.
|
||||
* Testing implementation details.
|
||||
* Ignoring error messages.
|
||||
* Not using custom commands.
|
||||
* Not using fixtures.
|
||||
* Trying to test across different domains (Cypress has limitations here).
|
||||
|
||||
### Edge Cases
|
||||
|
||||
* Handling complex UI interactions (e.g., drag-and-drop, file uploads).
|
||||
* Testing asynchronous operations.
|
||||
* Testing WebSockets.
|
||||
* Testing iframes.
|
||||
* Testing third-party integrations.
|
||||
|
||||
### Version-Specific Issues
|
||||
|
||||
* Be aware of breaking changes in new versions of Cypress. Refer to the Cypress changelog for details.
|
||||
* Test your tests after upgrading Cypress.
|
||||
|
||||
### Compatibility Concerns
|
||||
|
||||
* Cypress primarily supports Chromium-based browsers (Chrome, Edge). Limited support for Firefox and experimental support for Safari.
|
||||
* Be aware of compatibility issues between Cypress and other technologies (e.g., React, Angular, Vue.js).
|
||||
|
||||
### Debugging Strategies
|
||||
|
||||
* Use the Cypress Test Runner to step through your tests and inspect the application state.
|
||||
* Use the browser's developer tools to debug your tests.
|
||||
* Use `console.log()` statements to log data to the console.
|
||||
* Use `cy.pause()` to pause test execution and inspect the application state.
|
||||
* Use Cypress Cloud for advanced debugging features such as time travel and video recording.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
### Recommended Development Tools
|
||||
|
||||
* **IDE:** Visual Studio Code with the Cypress extension.
|
||||
* **Browser:** Chrome or Edge.
|
||||
* **Node.js:** Latest LTS version.
|
||||
* **NPM or Yarn:** Package managers.
|
||||
|
||||
### Build Configuration
|
||||
|
||||
* Use a bundler (e.g., Webpack, Parcel) to bundle your test files.
|
||||
* Configure your bundler to optimize your test files for performance.
|
||||
* Use environment variables to configure your tests for different environments.
|
||||
|
||||
### Linting and Formatting
|
||||
|
||||
* Use ESLint and Prettier to lint and format your code.
|
||||
* Configure ESLint and Prettier to enforce consistent coding style.
|
||||
* Use the Cypress ESLint plugin to catch common Cypress-specific errors.
|
||||
|
||||
### Deployment
|
||||
|
||||
* Run your Cypress tests as part of your CI/CD pipeline.
|
||||
* Use Cypress Cloud to run your tests in parallel and get detailed test results.
|
||||
* Deploy your Cypress tests to a staging environment before deploying to production.
|
||||
|
||||
### CI/CD Integration
|
||||
|
||||
* Integrate Cypress with your CI/CD pipeline (e.g., GitHub Actions, Jenkins, CircleCI).
|
||||
* Run your Cypress tests automatically on every commit or pull request.
|
||||
* Use Cypress Cloud to store your test results and track test history.
|
||||
* Configure your CI/CD pipeline to fail if your Cypress tests fail.
|
||||
|
||||
By adhering to these best practices, you can create a robust and maintainable Cypress test suite that ensures the quality of your web application.
|
||||
}, {
|
||||
cacheAcrossSpecs: true // Cache across spec files for even faster runs
|
||||
});
|
||||
});
|
||||
|
||||
// In your test file
|
||||
describe('Authenticated Features', () => {
|
||||
beforeEach(() => {
|
||||
cy.apiLogin('user@example.com', 'password123');
|
||||
cy.visit('/dashboard');
|
||||
});
|
||||
// ... tests ...
|
||||
});
|
||||
```
|
||||
|
||||
## 7. Test Naming & Readability
|
||||
|
||||
Write clear, descriptive test names that explain the intent and expected outcome.
|
||||
|
||||
❌ **BAD:** Vague test names
|
||||
```typescript
|
||||
it('test 1', () => { /* ... */ });
|
||||
it('login works', () => { /* ... */ });
|
||||
```
|
||||
|
||||
✅ **GOOD:** Descriptive test names
|
||||
```typescript
|
||||
describe('Authentication', () => {
|
||||
it('should allow a valid user to log in and redirect to dashboard', () => { /* ... */ });
|
||||
context('when credentials are invalid', () => {
|
||||
it('should display an error message for incorrect password', () => { /* ... */ });
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
## 8. Avoiding External Sites
|
||||
|
||||
Keep E2E tests focused on our application. If an external origin is unavoidable, use `cy.origin()`.
|
||||
|
||||
❌ **BAD:** Automating external sites directly
|
||||
```typescript
|
||||
cy.visit('https://our-app.com');
|
||||
cy.get('.external-link').click();
|
||||
cy.url().should('include', 'external-provider.com');
|
||||
cy.get('#external-form').type('data'); // Unreliable, out of our control
|
||||
```
|
||||
|
||||
✅ **GOOD:** Using `cy.origin()` for controlled external interactions
|
||||
```typescript
|
||||
it('should handle OAuth redirect', () => {
|
||||
cy.visit('/login');
|
||||
cy.getBySel('oauth-login-button').click();
|
||||
|
||||
cy.origin('https://external-oauth.com', () => {
|
||||
cy.get('#username').type('oauth_user');
|
||||
cy.get('#password').type('oauth_pass');
|
||||
cy.get('#submit').click();
|
||||
});
|
||||
|
||||
cy.url().should('include', '/dashboard'); // Back to our app
|
||||
});
|
||||
```
|
||||
Reference in New Issue
Block a user