Test Framework
The project uses Karma as the test runner and Jasmine as the testing framework. These are industry-standard tools for Angular application testing.Testing Stack
- Karma 6.4.0: Test runner that executes tests in real browsers
- Jasmine 5.9.0: BDD-style testing framework
- karma-chrome-launcher: Runs tests in Chrome/Chromium
- karma-coverage: Generates code coverage reports
- karma-jasmine-html-reporter: Pretty HTML test results
Running Tests
Execute the test suite using:- Compiles the application and test files
- Launches a Chrome browser window
- Runs all
*.spec.tsfiles - Watches for file changes and re-runs tests automatically
- Displays results in both terminal and browser
Tests run in watch mode by default. The test runner will automatically re-execute tests when you modify source or test files.
Test Configuration
angular.json
The test configuration is defined in the Angular workspace:angular.json
tsconfig.spec.json
TypeScript configuration for test files:tsconfig.spec.json
- Extends base TypeScript configuration
- Includes Jasmine type definitions
- Compiles all
.tsfiles insrc/directory - Outputs to
out-tsc/spec/
Test Files
The project includes the following spec files:Component Tests
src/app/components/about/about.spec.ts- About section testssrc/app/components/contact/contact.spec.ts- Contact form testssrc/app/components/footer/footer.spec.ts- Footer component testssrc/app/components/header/header.spec.ts- Header navigation testssrc/app/components/hero/hero.spec.ts- Hero section testssrc/app/components/projects/projects.spec.ts- Projects showcase tests
Application Tests
src/app/app.spec.ts- Root component tests
Each component has a corresponding
.spec.ts file in the same directory, following Angular’s testing best practices.Writing Tests
Basic Test Structure
Component tests typically follow this pattern:Test File Naming
All test files must:- End with
.spec.tsextension - Be located alongside the component they test
- Match the component filename (e.g.,
header.ts→header.spec.ts)
Test Output
Terminal Output
Test results are displayed in the terminal:Browser Output
A Chrome window opens showing:- Test suite hierarchy
- Individual test results (pass/fail)
- Failure details and stack traces
- Ability to re-run specific tests
Coverage Reports
Code coverage reports are generated by karma-coverage and can be configured to output HTML reports showing:- Line coverage
- Branch coverage
- Function coverage
- Statement coverage
Test Commands
Single Run
Run tests once without watch mode (useful for CI/CD):Specific Browser
Run tests in a specific browser:Code Coverage
Generate code coverage report:coverage/ directory.
Best Practices
- Test component creation: Always verify the component instantiates correctly
- Test user interactions: Simulate clicks, input changes, and form submissions
- Test rendering: Verify DOM elements display expected content
- Test business logic: Unit test component methods and calculations
- Mock dependencies: Use Jasmine spies to isolate component logic
- Keep tests focused: One test should verify one behavior
- Use descriptive test names: Test descriptions should explain what is being tested
Next Steps
- Learn about the build process
- Review component architecture
- Set up continuous integration with automated testing