This document provides comprehensive information about the test suite for the Angular UI application, which has been updated to Angular 19.0.0.
- Test Structure
- Running Tests
- Test Types
- Test Coverage
- Writing New Tests
- CI/CD Integration
- Troubleshooting
The test suite is organized into the following categories:
Located in src/app/*.spec.ts
- app.component.spec.ts: Main component unit tests
- app.service.spec.ts: Service layer tests
- form-validation.spec.ts: Form validation tests
- captcha.service.spec.ts: Captcha functionality tests
- performance.spec.ts: Performance benchmarking tests
Located in src/app/*.integration.spec.ts
- login.integration.spec.ts: Full login flow integration tests
Located in e2e/src/*.e2e-spec.ts
- login.e2e-spec.ts: End-to-end login tests
npm testnpm run test:cinpm run test:coveragenpm run e2eng test --include='**/app.component.spec.ts'ng test --watchTests individual component behavior, methods, and properties.
Example:
it('should initialize with null captcha', () => {
expect(component.captcha).toBeNull();
});Tests all form validation rules and error messages.
Coverage:
- Empty field validation
- Missing field validation
- Valid input acceptance
- Error message accuracy
Tests complete workflows and interactions between components.
Coverage:
- Full login flow
- Captcha refresh during login
- Error recovery flows
- State consistency
Benchmarks component performance and identifies bottlenecks.
Coverage:
- Component initialization time
- Form validation speed
- Memory usage
- Change detection performance
- Rendering performance
Tests user interactions from a browser perspective.
Coverage:
- Page load behavior
- Form interactions
- Button clicks
- Error display
- Responsive design
- Statements: 80%
- Branches: 75%
- Functions: 80%
- Lines: 80%
After running npm run test:coverage, open:
coverage/AngularUI/index.html
- Unit Tests: 100+ test cases
- Integration Tests: 30+ test cases
- E2E Tests: 40+ test cases
- Performance Tests: 20+ test cases
- Unit tests:
*.spec.ts - Integration tests:
*.integration.spec.ts - E2E tests:
*.e2e-spec.ts
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { YourComponent } from './your-component';
describe('YourComponent', () => {
let component: YourComponent;
let fixture: ComponentFixture<YourComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [YourComponent],
imports: [/* required modules */]
}).compileComponents();
fixture = TestBed.createComponent(YourComponent);
component = fixture.componentInstance;
});
it('should create', () => {
expect(component).toBeTruthy();
});
// Add more tests here
});-
Use Descriptive Test Names
// Good it('should display error when username is missing', () => {}); // Bad it('should work', () => {});
-
Follow AAA Pattern
- Arrange: Set up test data
- Act: Execute the code
- Assert: Verify the result
-
Test One Thing at a Time Each test should verify a single behavior.
-
Use beforeEach for Setup Common setup code should be in beforeEach blocks.
-
Clean Up After Tests Use afterEach to clean up mocks and spies.
-
Mock External Dependencies Always mock HTTP calls, external services, etc.
Located at: src/karma.conf.js
Key Settings:
- Framework: Jasmine
- Browsers: Chrome, ChromeHeadless
- Coverage Tool: karma-coverage
- Reporters: progress, kjhtml, coverage
Located at: src/tsconfig.spec.json
Includes test-specific TypeScript settings.
name: Run Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '20'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm run test:ci
- name: Generate coverage
run: npm run test:coverage
- name: Upload coverage
uses: codecov/codecov-action@v2pipeline {
agent any
stages {
stage('Install') {
steps {
sh 'npm ci'
}
}
stage('Test') {
steps {
sh 'npm run test:ci'
}
}
stage('Coverage') {
steps {
sh 'npm run test:coverage'
publishHTML([
reportDir: 'coverage/AngularUI',
reportFiles: 'index.html',
reportName: 'Coverage Report'
])
}
}
}
}Error: Chrome binary not found
Solution:
# Use ChromeHeadless instead
npm run test:ciError: Timeout - Async callback was not invoked
Solution:
- Increase timeout in karma.conf.js
- Ensure all async operations complete
- Use
fakeAsyncandtick()for testing async code
Error: Cannot find module
Solution:
- Ensure all required modules are imported in TestBed
- Check that paths in tsconfig are correct
- Run
npm installto ensure dependencies are installed
Error: Coverage thresholds not met
Solution:
- Identify uncovered code in coverage report
- Add tests for uncovered branches
- Remove dead code if not needed
Error: JavaScript heap out of memory
Solution:
# Increase memory limit
export NODE_OPTIONS="--max-old-space-size=4096"
npm testRun tests with debugging:
# Chrome DevTools
ng test --browsers=Chrome --watch
# VS Code debugging
# Add launch.json configuration:
{
"type": "chrome",
"request": "attach",
"name": "Karma Tests",
"address": "localhost",
"port": 9876,
"pathMapping": {
"/": "${workspaceRoot}",
"/base/": "${workspaceRoot}/"
}
}- Weekly: Review failing tests
- Monthly: Update test dependencies
- Quarterly: Review and update test coverage goals
- After Major Changes: Update integration tests
When upgrading Angular:
- Update testing dependencies in package.json
- Update karma.conf.js if needed
- Run tests to identify breaking changes
- Update test code to use new APIs
- Verify coverage remains above thresholds
- Component initialization: < 100ms
- Form validation: < 10ms per validation
- Change detection: < 50ms per cycle
- E2E page load: < 3 seconds
Run performance tests regularly:
ng test --include='**/performance.spec.ts'- Angular CLI
- Chrome DevTools
- Wallaby.js - Live test runner
For questions or issues:
- Check this documentation first
- Review Angular testing documentation
- Search existing GitHub issues
- Create a new issue with reproduction steps
- Updated to Angular 19.0.0
- Added 190+ comprehensive test cases
- Implemented performance testing suite
- Enhanced integration test coverage
- Updated E2E tests with accessibility checks
- Improved code coverage to 80%+
- v7.2.0: Initial test implementation