Sub topic 13
Running unit tests with ng test
This chapter assumes you can read angular.json and know what a tsconfig file is. It also assumes you have generated a component with ng generate. Those topics are covered in earlier chapters.
What ng test does
ng test runs the unit tests in your workspace. It reads the test target in angular.json, compiles your *.spec.ts files with tsconfig.spec.json, and runs them in a browser. Projects created with Angular 17 to 19 use Karma as the test runner and Jasmine as the test framework by default. Newer CLI versions may generate a different runner, so check the builder value in your own angular.json.
The test target
The test target sits beside build in the architect section. This excerpt shows the parts you edit most often:
{
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"polyfills": ["zone.js", "zone.js/testing"],
"tsConfig": "tsconfig.spec.json",
"styles": ["src/styles.css"]
}
}
}tsConfigpoints totsconfig.spec.json. That file extends the basetsconfig.jsonand adds the Jasmine type definitions.polyfillsloadszone.js/testing, which the Angular test utilities need.stylesapplies global styles to the tests, just as it does in the build.
A generated spec file
ng generate component creates a spec file beside the component. The file name and class name below follow the older convention. Newer CLI versions drop the .component suffix, so the names become product-list.ts and ProductList.
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ProductListComponent } from './product-list.component';
describe('ProductListComponent', () => {
let fixture: ComponentFixture<ProductListComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ProductListComponent],
}).compileComponents();
fixture = TestBed.createComponent(ProductListComponent);
fixture.detectChanges();
});
it('should create', () => {
expect(fixture.componentInstance).toBeTruthy();
});
});The component is standalone, so it goes in imports, not declarations. TestBed builds a small test module around it. detectChanges() runs change detection so the template renders.
Useful options
ng test
ng test --watch=false --browsers=ChromeHeadless
ng test --include='src/app/products/**/*.spec.ts'
ng test --code-coverage- The first command opens a browser and re-runs the tests when you save a file.
- The second runs once in a headless browser and exits. Use it in continuous integration (CI), where no display is available.
--includelimits the run to matching spec files.--code-coveragewrites a report to acoveragefolder.
Common errors
- No provider for a service. The component under test injects a service that the test module does not provide. Add the provider, or a fake, to
TestBed.configureTestingModule. - The test hangs in CI. Without
--watch=false, the runner waits for file changes and never exits. - No Chrome binary found. Headless runs need Chrome or Chromium installed on the machine.
- A spec is not picked up. The file name must end in
.spec.ts, andtsconfig.spec.jsonmust include it.
Exercise
Your CI job runs ng test and never finishes. Which two options do you add so it runs once without opening a visible browser window?
Show answer
Add --watch=false so the runner exits after one pass, and --browsers=ChromeHeadless so the tests run without a visible browser window.