Sub topic 13
Coverage and CI
This chapter assumes you can already run ng test and write component tests with TestBed, as covered earlier. You also need npm and a Git host that can run a pipeline. Here you will measure how much of your code the tests run, set a minimum, and run the suite in continuous integration (CI).
What coverage measures
Coverage reports which parts of your source ran while the tests executed. Vitest tracks four numbers:
| Metric | Counts |
|---|---|
| Statements | Executed statements |
| Branches | Each side of `if`, `?:`, `@if`, and similar |
| Functions | Functions that were called at least once |
| Lines | Source lines that ran |
Coverage shows what your tests did not touch. It does not show that the tests check anything useful. A test that renders a component and asserts nothing still produces coverage.
Turning coverage on
Vitest needs a coverage provider package. Install the V8 provider as a development dependency:
npm install --save-dev @vitest/coverage-v8Then add the flag when you run the tests:
ng test --coverageThe builder prints a table in the terminal and writes reports to a coverage folder. Open the HTML report in a browser to see each file with executed and missed lines highlighted.
To make coverage the default, set it in the test target of angular.json:
{
"test": {
"builder": "@angular/build:unit-test",
"options": {
"runner": "vitest",
"coverage": true,
"coverageReporters": ["html", "lcov"],
"coverageExclude": ["src/**/*.config.ts", "src/main.ts"]
}
}
}coverageReporters chooses the output formats. html is for people. lcov is read by most CI dashboards and coverage services. coverageExclude removes files that are wiring rather than behavior.
Setting a threshold
A threshold makes ng test fail when coverage drops below a number you choose:
{
"test": {
"builder": "@angular/build:unit-test",
"options": {
"runner": "vitest",
"coverage": true,
"coverageThresholds": {
"statements": 80,
"branches": 70,
"functions": 80,
"lines": 80
}
}
}
}Branches usually sit lower than lines, because error paths are harder to reach. Start with the numbers you have today and raise them slowly. A threshold that is far above reality gets switched off. A threshold that is too low protects nothing.
Running in CI
A CI job should behave the same on every run. It must not wait for file changes, and it must not need a display. Use these rules:
- Install exactly what the lock file says with
npm ci. - Pass
--no-watchso the run ends when the tests finish. - Pass
--coverageso thresholds are enforced. - Keep the exit code. A failing test or threshold makes
ng testexit non-zero, and that fails the job.
Here is a GitHub Actions workflow that follows these rules:
name: test
on:
pull_request:
push:
branches: [main]
jobs:
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npx ng test --no-watch --coverage
- uses: actions/upload-artifact@v4
if: always()
with:
name: coverage
path: coverageif: always() uploads the report even when the tests fail, which is when you most want to read it.
Common problems
- The run never ends. Watch mode is on. Add
--no-watch. - "Cannot find coverage provider".
@vitest/coverage-v8is not installed, or its version does not match your Vitest version. - Generated or config files lower the numbers. Exclude them with
coverageExcludeinstead of lowering the thresholds. - Passes locally, fails in CI. Check the Node version and that CI uses
npm ci. Also look for tests that depend on the current time or on test order.
Exercise
Your CI job passes, but you want it to fail whenever branch coverage falls below 75%. Which setting do you add, and which command-line flags must the CI run still include?
Show answer
Add "coverageThresholds": { "branches": 75 } to the test options in angular.json. The CI command must still include --coverage, so coverage is collected and checked, and --no-watch, so the run ends. A failed threshold makes ng test exit non-zero, which fails the job.