End to End Testing
This project uses Cypress for comprehensive end-to-end testing to ensure the application works correctly from a user's perspective.
Prerequisites
Before running Cypress tests, ensure you have the following setup:
Talawa API Setup
- Important: The Talawa API must be properly installed, configured, and running before executing any Cypress tests. The tests depend on API endpoints being available and functional.
- Please follow the complete installation guide at: https://github.com/PalisadoesFoundation/talawa-api/blob/develop/INSTALLATION.md
Application Server
Ensure your local development server is running on http://localhost:4321.
Directory Structure
The tests follow the Page Object Model pattern for maintainability.
cypress/
├── e2e/ # End-to-end test specifications
│ └── example_spec/ # Related tests
├── fixtures/ # Test data and mock files
│ └── users.json # User test data
├── pageObjects/ # Page Object Model files
│ └── auth/ # Authentication page objects
└── support/ # Support files and custom commands
└── commands.ts # Custom Cypress commands
Key Components:
- e2e/: Contains all test specification files organized by feature
- fixtures/: Static data used in tests (JSON files, images, etc.)
- pageObjects/: Page Object Model implementation for maintainable test code
- support/: Custom commands and utilities to extend Cypress functionality
Running Tests
Follow these steps to run end to end tests
Available Commands
# Open Cypress Test Runner (Interactive Mode)
# Preferred for Debugging
pnpm run cy:open
# Run all tests in headless mode
pnpm run cy:run
Running Specific Tests
There are multiple testing modes.
Interactive Mode
For running specific tests with visual feedback, use the Interactive Mode where you can view all test specs and run individual tests:
pnpm run cy:open
Headless Mode
For running specific tests in headless mode, first manually start your application at http://localhost:4321, then use the following commands:
# Run tests in a specific folder
pnpm run cypress:run --spec "cypress/e2e/dashboard_spec/**/*"
# Run a specific test file
pnpm run cypress:run --spec "cypress/e2e/login_spec/login.cy.ts"
Writing Tests
Follow these best practices when writing tests.
Page Object Model
This project follows the Page Object Model pattern for better test maintenance:
// Example usage of page objects
import { LoginPage } from '../pageObjects/auth/LoginPage';
const loginPage = new LoginPage();
it('should login successfully', () => {
loginPage.verifyLoginPage().login(userData.email, userData.password);;
});
Custom Commands
Custom Cypress commands are defined in cypress/support/commands.ts to provide reusable functionality across tests.
Test Data
Use fixtures for consistent test data:
// Load test data from fixtures
cy.fixture('users').then((users) => {
// Use users data in tests
});
Test Coverage Report
After running your Cypress tests, you can generate detailed HTML coverage reports to analyze code coverage:
-
Run Cypress tests to collect coverage data:
pnpm run cy:run -
Generate HTML coverage report using nyc:
npx nyc --reporter=html -
View the coverage report in your browser:
open coverage/index.html
The HTML report provides an interactive view of:
- Overall coverage percentages (statements, branches, functions, lines)
- File-by-file coverage breakdown
- Detailed line-by-line coverage highlighting
- Uncovered code sections for easy identification
Note: Coverage data is collected during test execution and stored in the .nyc_output directory. The HTML report is generated in the coverage/ directory.
Contributing
When adding new tests:
- Follow the existing directory structure
- Use Page Object Model pattern
- Add appropriate fixtures for test data
- Ensure tests are independent and repeatable
- Document any new custom commands
For more information about Cypress testing, visit the official Cypress documentation.