Skip to content

Commit 0bca6ad

Browse files
committed
docs: Update testing guides for Vitest and Karma
This commit updates the testing documentation to reflect the introduction of Vitest as the default test runner, while ensuring Karma testing instructions are still available. Key changes include: - Adding a new guide for "Testing with Karma" (`guide/testing/karma.md`). - Updating the "Code Coverage" guide (`guide/testing/code-coverage.md`) to use `angular.json` for coverage enforcement and referencing Vitest documentation. - Modifying the "Testing Overview" guide (`guide/testing/overview.md`) to introduce Vitest as the default, update `ng test` output examples, and link to the new Karma guide. - Updating navigation data to include the new Karma testing guide.
1 parent ff3013c commit 0bca6ad

File tree

6 files changed

+280
-79
lines changed

6 files changed

+280
-79
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,11 @@ const DOCS_SUB_NAVIGATION_DATA: NavigationItem[] = [
526526
path: 'guide/testing/code-coverage',
527527
contentPath: 'guide/testing/code-coverage',
528528
},
529+
{
530+
label: 'Testing with Karma and Jasmine',
531+
path: 'guide/testing/karma',
532+
contentPath: 'guide/testing/karma',
533+
},
529534
{
530535
label: 'Testing services',
531536
path: 'guide/testing/services',

adev/src/content/guide/testing/code-coverage.md

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,59 @@ Code coverage reports show you any parts of your code base that might not be pro
55

66
To generate a coverage report run the following command in the root of your project.
77

8-
<docs-code language="shell">
9-
ng test --no-watch --code-coverage
10-
</docs-code>
8+
```shell
9+
ng test --no-watch --coverage
10+
```
1111

1212
When the tests are complete, the command creates a new `/coverage` directory in the project.
1313
Open the `index.html` file to see a report with your source code and code coverage values.
1414

1515
If you want to create code-coverage reports every time you test, set the following option in the Angular CLI configuration file, `angular.json`:
1616

17-
<docs-code language="json">
18-
"test": {
19-
"options": {
20-
"codeCoverage": true
17+
```json
18+
{
19+
"projects": {
20+
"your-project-name": {
21+
"architect": {
22+
"test": {
23+
"options": {
24+
"coverage": true
25+
}
26+
}
27+
}
28+
}
2129
}
2230
}
23-
</docs-code>
31+
```
2432

2533
## Code coverage enforcement
2634

2735
The code coverage percentages let you estimate how much of your code is tested.
28-
If your team decides on a set minimum amount to be unit tested, enforce this minimum with the Angular CLI.
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.
2937

3038
For example, suppose you want the code base to have a minimum of 80% code coverage.
31-
To enable this, open the [Karma](https://karma-runner.github.io) test platform configuration file, `karma.conf.js`, and add the `check` property in the `coverageReporter:` key.
32-
33-
<docs-code language="javascript">
34-
coverageReporter: {
35-
dir: require('path').join(__dirname, './coverage/<project-name>'),
36-
subdir: '.',
37-
reporters: [
38-
{ type: 'html' },
39-
{ type: 'text-summary' }
40-
],
41-
check: {
42-
global: {
43-
statements: 80,
44-
branches: 80,
45-
functions: 80,
46-
lines: 80
39+
To enable this, open the `angular.json` file and add the `coverageThresholds` option to your test configuration:
40+
41+
```json
42+
{
43+
"projects": {
44+
"your-project-name": {
45+
"architect": {
46+
"test": {
47+
"options": {
48+
"coverage": true,
49+
"coverageThresholds": {
50+
"statements": 80,
51+
"branches": 80,
52+
"functions": 80,
53+
"lines": 80
54+
}
55+
}
56+
}
57+
}
4758
}
4859
}
4960
}
50-
</docs-code>
51-
52-
HELPFUL: Read more about creating and fine tuning Karma configuration in the [testing guide](guide/testing#configuration).
53-
54-
The `check` property causes the tool to enforce a minimum of 80% code coverage when the unit tests are run in the project.
61+
```
5562

56-
Read more on coverage configuration options in the [karma coverage documentation](https://github.com/karma-runner/karma-coverage/blob/master/docs/configuration.md).
63+
Now, when you run `ng test`, the tool will throw an error if the coverage drops below 80%.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
If your tests aren't working as you expect them to, you can inspect and debug them in the browser.
44

5-
Debug specs in the browser in the same way that you debug an application.
5+
NOTE: This guide describes debugging with the Karma test runner.
6+
7+
To debug an application with the Karma test runner:
68

79
1. Reveal the Karma browser window.
810
See [Set up testing](guide/testing#set-up-testing) if you need help with this step.
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# Testing with Karma and Jasmine
2+
3+
While [Vitest](https://vitest.dev) is the default test runner for new Angular projects, [Karma](https://karma-runner.github.io) is still a supported and widely used test runner. This guide provides instructions for testing your Angular application using the Karma test runner with the [Jasmine](https://jasmine.github.io) testing framework.
4+
5+
## Setting Up Karma and Jasmine
6+
7+
You can set up Karma and Jasmine for a new project or add it to an existing one.
8+
9+
### For New Projects
10+
11+
To create a new project with Karma and Jasmine pre-configured, run the `ng new` command with the `--test-runner=karma` option:
12+
13+
```shell
14+
ng new my-karma-app --test-runner=karma
15+
```
16+
17+
### For Existing Projects
18+
19+
To add Karma and Jasmine to an existing project, follow these steps:
20+
21+
1. **Install the necessary packages:**
22+
23+
<docs-code-multifile>
24+
<docs-code header="pnpm" language="shell">
25+
pnpm add -D karma karma-chrome-launcher karma-coverage karma-jasmine karma-jasmine-html-reporter jasmine-core @types/jasmine
26+
</docs-code>
27+
<docs-code header="npm" language="shell">
28+
npm install --save-dev karma karma-chrome-launcher karma-coverage karma-jasmine karma-jasmine-html-reporter jasmine-core @types/jasmine
29+
</docs-code>
30+
<docs-code header="yarn" language="shell">
31+
yarn add --dev karma karma-chrome-launcher karma-coverage karma-jasmine karma-jasmine-html-reporter jasmine-core @types/jasmine
32+
</docs-code>
33+
</docs-code-multifile>
34+
35+
2. **Configure the test runner in `angular.json`:**
36+
37+
In your `angular.json` file, find the `test` target and set the `runner` option to `karma`:
38+
39+
```json
40+
{
41+
// ...
42+
"projects": {
43+
"your-project-name": {
44+
// ...
45+
"architect": {
46+
"test": {
47+
"builder": "@angular/build:unit-test",
48+
"options": {
49+
"runner": "karma",
50+
// ... other options
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}
57+
```
58+
59+
3. **Update `tsconfig.spec.json` for Jasmine types:**
60+
61+
To ensure TypeScript recognizes global testing functions like `describe` and `it`, add `"jasmine"` to the `types` array in your `tsconfig.spec.json`:
62+
63+
```json
64+
{
65+
// ...
66+
"compilerOptions": {
67+
// ...
68+
"types": [
69+
"jasmine"
70+
]
71+
},
72+
// ...
73+
}
74+
```
75+
76+
## Running Tests
77+
78+
Once your project is configured, run the tests using the [`ng test`](cli/test) command:
79+
80+
```shell
81+
ng test
82+
```
83+
84+
The `ng test` command builds the application in _watch mode_ and launches the [Karma test runner](https://karma-runner.github.io).
85+
86+
The console output looks like below:
87+
88+
```shell
89+
90+
02 11 2022 09:08:28.605:INFO [karma-server]: Karma v6.4.1 server started at http://localhost:9876/
91+
02 11 2022 09:08:28.607:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
92+
02 11 2022 09:08:28.620:INFO [launcher]: Starting browser Chrome
93+
02 11 2022 09:08:31.312:INFO [Chrome]: Connected on socket -LaEYvD2R7MdcS0-AAAB with id 31534482
94+
Chrome: Executed 3 of 3 SUCCESS (0.193 secs / 0.172 secs)
95+
TOTAL: 3 SUCCESS
96+
97+
```
98+
99+
The test output is displayed in the browser using [Karma Jasmine HTML Reporter](https://github.com/dfederm/karma-jasmine-html-reporter).
100+
101+
<img alt="Jasmine HTML Reporter in the browser" src="assets/images/guide/testing/initial-jasmine-html-reporter.png">
102+
103+
Click on a test row to re-run just that test or click on a description to re-run the tests in the selected test group ("test suite").
104+
105+
Meanwhile, the `ng test` command is watching for changes. To see this in action, make a small change to a source file and save. The tests run again, the browser refreshes, and the new test results appear.
106+
107+
## Configuration
108+
109+
The Angular CLI takes care of Jasmine and Karma configuration for you. It constructs the full configuration in memory, based on options specified in the `angular.json` file.
110+
111+
### Customizing Karma Configuration
112+
113+
If you want to customize Karma, you can create a `karma.conf.js` by running the following command:
114+
115+
```shell
116+
117+
ng generate config karma
118+
119+
```
120+
121+
HELPFUL: Read more about Karma configuration in the [Karma configuration guide](http://karma-runner.github.io/6.4/config/configuration-file.html).
122+
123+
### Setting the Test Runner in `angular.json`
124+
125+
To explicitly set Karma as the test runner for your project, locate the `test` target in your `angular.json` file and set the `runner` option to `karma`:
126+
127+
```json
128+
{
129+
// ...
130+
"projects": {
131+
"your-project-name": {
132+
// ...
133+
"architect": {
134+
"test": {
135+
"builder": "@angular/build:unit-test",
136+
"options": {
137+
"runner": "karma",
138+
// ... other options
139+
}
140+
}
141+
}
142+
}
143+
}
144+
}
145+
```
146+
147+
## Code coverage enforcement
148+
149+
To enforce a minimum code coverage level, you can use the `check` property in the `coverageReporter` section of your `karma.conf.js` file.
150+
151+
For example, to require a minimum of 80% coverage:
152+
153+
```javascript
154+
coverageReporter: {
155+
dir: require('path').join(__dirname, './coverage/<project-name>'),
156+
subdir: '.',
157+
reporters: [
158+
{ type: 'html' },
159+
{ type: 'text-summary' }
160+
],
161+
check: {
162+
global: {
163+
statements: 80,
164+
branches: 80,
165+
functions: 80,
166+
lines: 80
167+
}
168+
}
169+
}
170+
```
171+
172+
This will cause the test run to fail if the specified coverage thresholds are not met.
173+
174+
## Testing in continuous integration
175+
176+
To run your Karma tests in a CI environment, use the following command:
177+
178+
```shell
179+
ng test --no-watch --no-progress --browsers=ChromeHeadless
180+
```
181+
182+
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.

0 commit comments

Comments
 (0)