Posted 12/21/2024
Karma Test Commands
This post summarizes commonly used commands for running Angular unit tests with Karma during local development and CI.
Run selected test
npm t -- --include src\app\shared\user\user.service.spec.ts
- npm t is an alias for npm test
- – forwards arguments to the underlying test runner (Karma)
- –include limits execution to the specified spec file
Run all tests
For projects generated by angular CLI.
ng test
This starts Karma in watch mode by default.
Watch mode
Explicit watch mode (useful when scripting):
ng test --watch
Run tests in headless mode (CI)
Recommended for CI pipelines:
ng test --watch=false --browsers=ChromeHeadless
Code coverage
ng test --code-coverage
The report is generated in: coverage/
Open coverage/index.html to inspect detailed metrics.