This document provides comprehensive information about the testing setup, coverage analysis, and best practices for the Community Car Client Application.
The application uses a comprehensive testing strategy with both unit tests and end-to-end (E2E) tests to ensure code quality and reliability.
- Unit Testing: Angular Karma + Jasmine
- E2E Testing: Cypress with code coverage
- Coverage Reporting: Istanbul (nyc) for E2E, Karma Coverage for unit tests
cypress/
├── e2e/ # E2E test files
├── fixtures/ # Test data fixtures
├── support/ # Cypress support files
└── config.ts # Cypress configuration
src/
├── **/*.spec.ts # Unit test files
└── **/*.cy.ts # Component test files
# Run unit tests once
npm run test:ci
# Run unit tests with coverage
ng test --watch=false --browsers=ChromeHeadless --code-coverage
# Run unit tests in watch mode
npm test# Run E2E tests
npm run cy:run
# Run E2E tests with coverage
npm run e2e:coverage
# Open Cypress Test Runner
npm run cy:open# Run component tests
npx cypress run --componentCoverage reports are generated in the coverage/ directory after running unit tests with the --code-coverage flag.
E2E coverage is configured using @cypress/code-coverage and reports are generated in coverage/e2e/.
- Unit Tests: Configured in
angular.jsonandkarma.conf.js - E2E Tests: Configured in
.nycrc.jsonandcypress.config.ts
- Located alongside the code they test with
.spec.tsextension - Follow the naming convention:
component.service.spec.ts - Use Jasmine framework with Angular Testing Utilities
- Located in
cypress/e2e/directory - Use descriptive names like
app.cy.ts - Test complete user workflows and critical paths
- Located alongside components with
.cy.tsextension - Test component behavior in isolation
- Test one thing at a time: Each test should focus on a single behavior
- Use descriptive test names: Clearly describe what the test is verifying
- Arrange, Act, Assert: Structure tests with clear setup, execution, and verification phases
- Mock dependencies: Use Angular's TestBed and mocking utilities
- Test edge cases: Include tests for error conditions and boundary values
- Test user journeys: Focus on complete workflows
- Use data-testid attributes: For reliable element selection
- Avoid flaky tests: Use proper waiting strategies
- Keep tests independent: Each test should be runnable in isolation
- Statements: > 80%
- Branches: > 75%
- Functions: > 85%
- Lines: > 80%
Tests are integrated into the CI/CD pipeline and run automatically on:
- Pull requests
- Main branch pushes
- Release builds
- Located in
.github/workflows/(if using GitHub Actions) - Runs both unit and E2E tests
- Generates coverage reports
- Fails build on test failures or coverage below thresholds
# Run specific test file
ng test --include='**/auth.service.spec.ts'
# Debug in browser
ng test --browsers=Chrome# Run specific test file
npx cypress run --spec 'cypress/e2e/app.cy.ts'
# Debug with Cypress UI
npm run cy:open- Use
cy.wait()sparingly, prefercy.get().should()for assertions - Avoid fixed timeouts, use dynamic waiting
- Ensure test data is properly isolated
- Review coverage reports regularly
- Add tests for uncovered code paths
- Consider if uncovered code is actually unreachable
- Keep unit tests fast (< 100ms each)
- Parallelize E2E tests when possible
- Use test skipping for slow or unstable tests during development
When adding new features:
- Write tests first (TDD approach)
- Ensure all tests pass
- Maintain or improve coverage percentages
- Update this documentation if testing patterns change