Skip to content

Commit c5519fd

Browse files
committed
docs: update testing guides for Vitest and reorder navigation
This commit updates the unit testing, code coverage, and debugging guides to align with the Vitest test runner, which is now the default for new Angular CLI projects. Key changes include: - **Unit Testing Overview**: Revised to focus on Vitest, including details on DOM emulation (jsdom/happy-dom), browser testing setup, and custom Vitest configuration via `runnerConfig`. A prominent note links to the Karma-to-Vitest migration guide. - **Code Coverage**: Updated to reflect Vitest-specific prerequisites (`@vitest/coverage-v8`), command-line flags, and `angular.json` configuration for coverage reporting and threshold enforcement. - **Debugging**: Completely rewritten to provide instructions for debugging Vitest tests in both Node.js and browser environments using the `--debug` flag. - **Navigation Reordering**: The 'Testing' section in `sub-navigation-data.ts` has been reordered to present a more logical educational flow, starting with core concepts and progressing to advanced topics and migration.
1 parent 373c101 commit c5519fd

File tree

6 files changed

+208
-95
lines changed

6 files changed

+208
-95
lines changed

adev/src/app/routing/sub-navigation-data.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -521,21 +521,6 @@ const DOCS_SUB_NAVIGATION_DATA: NavigationItem[] = [
521521
path: 'guide/testing',
522522
contentPath: 'guide/testing/overview',
523523
},
524-
{
525-
label: 'Code coverage',
526-
path: 'guide/testing/code-coverage',
527-
contentPath: 'guide/testing/code-coverage',
528-
},
529-
{
530-
label: 'Testing with Karma and Jasmine',
531-
path: 'guide/testing/karma',
532-
contentPath: 'guide/testing/karma',
533-
},
534-
{
535-
label: 'Testing services',
536-
path: 'guide/testing/services',
537-
contentPath: 'guide/testing/services',
538-
},
539524
{
540525
label: 'Basics of testing components',
541526
path: 'guide/testing/components-basics',
@@ -546,6 +531,11 @@ const DOCS_SUB_NAVIGATION_DATA: NavigationItem[] = [
546531
path: 'guide/testing/components-scenarios',
547532
contentPath: 'guide/testing/components-scenarios',
548533
},
534+
{
535+
label: 'Testing services',
536+
path: 'guide/testing/services',
537+
contentPath: 'guide/testing/services',
538+
},
549539
{
550540
label: 'Testing attribute directives',
551541
path: 'guide/testing/attribute-directives',
@@ -567,16 +557,16 @@ const DOCS_SUB_NAVIGATION_DATA: NavigationItem[] = [
567557
path: 'guide/testing/debugging',
568558
contentPath: 'guide/testing/debugging',
569559
},
560+
{
561+
label: 'Code coverage',
562+
path: 'guide/testing/code-coverage',
563+
contentPath: 'guide/testing/code-coverage',
564+
},
570565
{
571566
label: 'Testing utility APIs',
572567
path: 'guide/testing/utility-apis',
573568
contentPath: 'guide/testing/utility-apis',
574569
},
575-
{
576-
label: 'Migrating from Karma to Vitest',
577-
path: 'guide/testing/unit-tests',
578-
contentPath: 'guide/testing/migrating-to-vitest',
579-
},
580570
{
581571
label: 'Component harnesses overview',
582572
path: 'guide/testing/component-harnesses-overview',
@@ -597,6 +587,16 @@ const DOCS_SUB_NAVIGATION_DATA: NavigationItem[] = [
597587
path: 'guide/testing/component-harnesses-testing-environments',
598588
contentPath: 'guide/testing/component-harnesses-testing-environments',
599589
},
590+
{
591+
label: 'Migrating from Karma to Vitest',
592+
path: 'guide/testing/migrating-to-vitest',
593+
contentPath: 'guide/testing/migrating-to-vitest',
594+
},
595+
{
596+
label: 'Testing with Karma and Jasmine',
597+
path: 'guide/testing/karma',
598+
contentPath: 'guide/testing/karma',
599+
},
600600
],
601601
},
602602
{
Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
1-
# Find out how much code you're testing
1+
# Code coverage
22

3-
The Angular CLI can run unit tests and create code coverage reports.
43
Code coverage reports show you any parts of your code base that might not be properly tested by your unit tests.
54

6-
To generate a coverage report run the following command in the root of your project.
5+
## Prerequisites
6+
7+
To generate code coverage reports with Vitest, you must install the `@vitest/coverage-v8` package:
8+
9+
<docs-code-multifile>
10+
<docs-code header="pnpm" language="shell">
11+
pnpm add -D @vitest/coverage-v8
12+
</docs-code>
13+
<docs-code header="npm" language="shell">
14+
npm install --save-dev @vitest/coverage-v8
15+
</docs-code>
16+
<docs-code header="yarn" language="shell">
17+
yarn add --dev @vitest/coverage-v8
18+
</docs-code>
19+
</docs-code-multifile>
20+
21+
## Generating a report
22+
23+
To generate a coverage report, add the `--coverage` flag to the `ng test` command:
724

825
```shell
9-
ng test --no-watch --coverage
26+
ng test --coverage
1027
```
1128

12-
When the tests are complete, the command creates a new `/coverage` directory in the project.
13-
Open the `index.html` file to see a report with your source code and code coverage values.
29+
After the tests run, the command creates a new `coverage/` directory in the project. Open the `index.html` file to see a report with your source code and code coverage values.
1430

15-
If you want to create code-coverage reports every time you test, set the following option in the Angular CLI configuration file, `angular.json`:
31+
If you want to create code-coverage reports every time you test, you can set the `coverage` option to `true` in your `angular.json` file:
1632

1733
```json
1834
{
1935
"projects": {
2036
"your-project-name": {
2137
"architect": {
2238
"test": {
39+
"builder": "@angular/build:unit-test",
2340
"options": {
2441
"coverage": true
2542
}
@@ -30,20 +47,19 @@ If you want to create code-coverage reports every time you test, set the followi
3047
}
3148
```
3249

33-
## Code coverage enforcement
50+
## Enforcing code coverage thresholds
3451

35-
The code coverage percentages let you estimate how much of your code is tested.
36-
If your team decides on a set minimum amount to be unit tested, you can enforce this minimum directly in your Angular CLI configuration.
52+
The code coverage percentages let you estimate how much of your code is tested. If your team decides on a minimum amount to be unit tested, you can enforce this minimum in your configuration.
3753

38-
For example, suppose you want the code base to have a minimum of 80% code coverage.
39-
To enable this, open the `angular.json` file and add the `coverageThresholds` option to your test configuration:
54+
For example, suppose you want the code base to have a minimum of 80% code coverage. To enable this, add the `coverageThresholds` option to your `angular.json` file:
4055

4156
```json
4257
{
4358
"projects": {
4459
"your-project-name": {
4560
"architect": {
4661
"test": {
62+
"builder": "@angular/build:unit-test",
4763
"options": {
4864
"coverage": true,
4965
"coverageThresholds": {
@@ -60,4 +76,36 @@ To enable this, open the `angular.json` file and add the `coverageThresholds` op
6076
}
6177
```
6278

63-
Now, when you run `ng test`, the tool will throw an error if the coverage drops below 80%.
79+
Now, if your coverage drops below 80% when you run your tests, the command will fail.
80+
81+
## Advanced configuration
82+
83+
You can configure several other coverage options in your `angular.json` file:
84+
85+
- `coverageInclude`: Glob patterns of files to include in the coverage report.
86+
- `coverageReporters`: An array of reporters to use (e.g., `html`, `lcov`, `json`).
87+
- `coverageWatermarks`: An object specifying `[low, high]` watermarks for the HTML reporter, which can affect the color-coding of the report.
88+
89+
```json
90+
{
91+
"projects": {
92+
"your-project-name": {
93+
"architect": {
94+
"test": {
95+
"builder": "@angular/build:unit-test",
96+
"options": {
97+
"coverage": true,
98+
"coverageReporters": ["html", "lcov"],
99+
"coverageWatermarks": {
100+
"statements": [50, 80],
101+
"branches": [50, 80],
102+
"functions": [50, 80],
103+
"lines": [50, 80]
104+
}
105+
}
106+
}
107+
}
108+
}
109+
}
110+
}
111+
```
Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
# Debugging tests
22

3-
If your tests aren't working as you expect them to, you can inspect and debug them in the browser.
3+
If your tests aren't working as you expect, you can debug them in both the default Node.js environment and in a real browser.
44

5-
NOTE: This guide describes debugging with the Karma test runner.
5+
## Debugging in Node.js
66

7-
To debug an application with the Karma test runner:
7+
Debugging in the default Node.js environment is often the quickest way to diagnose issues that are not related to browser-specific APIs or rendering.
88

9-
1. Reveal the Karma browser window.
10-
See [Set up testing](guide/testing#set-up-testing) if you need help with this step.
9+
1. Run the `ng test` command with the `--debug` flag:
10+
```shell
11+
ng test --debug
12+
```
13+
2. The test runner will start in debug mode and wait for a debugger to attach.
14+
3. You can now attach your preferred debugger. For example, you can use the built-in Node.js debugger in VS Code or the Chrome DevTools for Node.js.
1115

12-
1. Click the **DEBUG** button to open a new browser tab and re-run the tests.
13-
1. Open the browser's **Developer Tools**. On Windows, press `Ctrl-Shift-I`. On macOS, press `Command-Option-I`.
14-
1. Pick the **Sources** section.
15-
1. Press `Control/Command-P`, and then start typing the name of your test file to open it.
16-
1. Set a breakpoint in the test.
17-
1. Refresh the browser, and notice how it stops at the breakpoint.
16+
## Debugging in a browser
1817

19-
<img alt="Karma debugging" src="assets/images/guide/testing/karma-1st-spec-debug.png">
18+
Debugging in a browser is recommended for tests that rely on the DOM or other browser-specific APIs. This approach allows you to use the browser's own developer tools.
19+
20+
1. Ensure you have a browser provider installed. See [Running tests in a browser](guide/testing/overview#running-tests-in-a-browser) for setup instructions.
21+
2. Run the `ng test` command with both the `--browsers` and `--debug` flags:
22+
```shell
23+
ng test --browsers=chromium --debug
24+
```
25+
3. This command runs the tests in a headed browser and keeps it open after the tests finish, allowing you to inspect the output.
26+
4. Open the browser's **Developer Tools**. On Windows, press `Ctrl-Shift-I`. On macOS, press `Command-Option-I`.
27+
5. Go to the **Sources** tab.
28+
6. Use `Control/Command-P` to search for and open your test file.
29+
7. Set a breakpoint in your test.
30+
8. Reload the test runner UI in the browser. The execution will now stop at your breakpoint.

adev/src/content/guide/testing/karma.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,19 @@ ng test --no-watch --no-progress --browsers=ChromeHeadless
180180
```
181181

182182
NOTE: The `--no-watch` and `--no-progress` flags are crucial for Karma in CI environments to ensure tests run once and exit cleanly. The `--browsers=ChromeHeadless` flag is also essential for running tests in a browser environment without a graphical interface.
183+
184+
## Debugging tests
185+
186+
If your tests aren't working as you expect, you can inspect and debug them in the browser.
187+
188+
To debug an application with the Karma test runner:
189+
190+
1. Reveal the Karma browser window. See [Set up for testing](guide/testing/overview#set-up-for-testing) if you need help with this step.
191+
2. Click the **DEBUG** button to open a new browser tab and re-run the tests.
192+
3. Open the browser's **Developer Tools**. On Windows, press `Ctrl-Shift-I`. On macOS, press `Command-Option-I`.
193+
4. Pick the **Sources** section.
194+
5. Press `Control/Command-P`, and then start typing the name of your test file to open it.
195+
6. Set a breakpoint in the test.
196+
7. Refresh the browser, and notice how it stops at the breakpoint.
197+
198+
<img alt="Karma debugging" src="assets/images/guide/testing/karma-1st-spec-debug.png">

0 commit comments

Comments
 (0)