Sub topic 2
Setting up and running tests with ng test
This chapter assumes you have an Angular workspace built with the Angular CLI, and that you know TypeScript and npm basics. You will check the test configuration, run the tests, and narrow a run to the specs you care about.
What ng test does
ng test does not run Vitest directly. It runs the test target in angular.json, and that target names a builder. A builder is the CLI plugin that does the work for a target. The @angular/build:unit-test builder compiles your spec files with the same toolchain as ng build, then hands the result to a test runner. The runner option picks that runner.
The test target
Open angular.json and find the test target of your project. For Vitest it looks like this:
{
"test": {
"builder": "@angular/build:unit-test",
"options": {
"runner": "vitest"
}
}
}This is the complete setup on the Angular side. Newer projects from ng new already have it. For an older project, change the builder and add the runner option.
Installing the packages
The builder needs Vitest and a simulated browser environment. By default it runs specs in jsdom, a Node.js implementation of the browser DOM. Install both as development dependencies:
npm install --save-dev vitest jsdomSpecs that render components work as soon as these are installed. The TestBed helper that creates components is covered in a later chapter.
Running the tests
Run the tests from the workspace root:
ng testIn an interactive terminal the command stays in watch mode. When you save a file, it reruns the affected specs. In a non-interactive environment such as a CI job, the run happens once and then exits. You can set this explicitly with a flag:
ng test --no-watchThe builder finds spec files with the pattern **/*.spec.ts. To run a subset, pass --include with a glob:
ng test --include='src/app/counter/**/*.spec.ts'Quote the glob so your shell does not expand it before the CLI sees it.
Importing Vitest functions
Vitest does not put describe, it, and expect in the global scope unless you configure it to. The examples in this tutorial import them explicitly:
import { describe, it, expect } from 'vitest';
describe('setup check', () => {
it('runs a spec through ng test', () => {
expect(1 + 1).toBe(2);
});
});Save this as src/app/setup-check.spec.ts and run ng test. One passing test confirms that the builder, the runner, and TypeScript compilation all work.
Common setup errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Cannot find package `vitest` | Package not installed | Run `npm install --save-dev vitest` |
| Cannot find package `jsdom` | No DOM environment installed | Run `npm install --save-dev jsdom` |
| No tests found | File name does not end in `.spec.ts`, or `--include` matches nothing | Rename the file or check the glob |
| Run never exits in CI | Watch mode is on | Add `--no-watch` |
| `describe` or `it` is not defined | Globals used without imports | Import from `vitest` |
Exercise
A teammate's CI job runs ng test and hangs after the tests pass. Which flag fixes it, and what does the flag change?
Show answer
Use ng test --no-watch. It makes the builder run all specs once and exit, instead of staying in watch mode and waiting for file changes.
Exercise
Which angular.json option makes ng test use Vitest, and which builder reads that option?
Show answer
The runner option set to "vitest" in the test target's options, read by the @angular/build:unit-test builder.