Skip to content

Commit 0080c60

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 c84d372 commit 0080c60

File tree

6 files changed

+199
-54
lines changed

6 files changed

+199
-54
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,11 @@ const DOCS_SUB_NAVIGATION_DATA: NavigationItem[] = [
522522
path: 'guide/testing/code-coverage',
523523
contentPath: 'guide/testing/code-coverage',
524524
},
525+
{
526+
label: 'Testing with Karma',
527+
path: 'guide/testing/karma',
528+
contentPath: 'guide/testing/karma',
529+
},
525530
{
526531
label: 'Testing services',
527532
path: 'guide/testing/services',

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

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Code coverage reports show you any parts of your code base that might not be pro
77
To generate a coverage report run the following command in the root of your project.
88

99
<docs-code language="shell">
10-
ng test --no-watch --code-coverage
10+
ng test --no-watch --coverage
1111
</docs-code>
1212

1313
When the tests are complete, the command creates a new `/coverage` directory in the project.
@@ -18,40 +18,33 @@ If you want to create code-coverage reports every time you test, set the followi
1818
<docs-code language="json">
1919
"test": {
2020
"options": {
21-
"codeCoverage": true
21+
"coverage": true
2222
}
2323
}
2424
</docs-code>
2525

2626
## Code coverage enforcement
2727

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

3131
For example, suppose you want the code base to have a minimum of 80% code coverage.
32-
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.
33-
34-
<docs-code language="javascript">
35-
coverageReporter: {
36-
dir: require('path').join(__dirname, './coverage/<project-name>'),
37-
subdir: '.',
38-
reporters: [
39-
{ type: 'html' },
40-
{ type: 'text-summary' }
41-
],
42-
check: {
43-
global: {
44-
statements: 80,
45-
branches: 80,
46-
functions: 80,
47-
lines: 80
32+
To enable this, open the `angular.json` file and add the `coverageThresholds` option to your test configuration:
33+
34+
<docs-code language="json">
35+
"test": {
36+
"options": {
37+
"coverage": true,
38+
"coverageThresholds": {
39+
"statements": 80,
40+
"branches": 80,
41+
"functions": 80,
42+
"lines": 80
4843
}
4944
}
5045
}
5146
</docs-code>
5247

53-
HELPFUL: Read more about creating and fine tuning Karma configuration in the [testing guide](guide/testing#configuration).
54-
55-
The `check` property causes the tool to enforce a minimum of 80% code coverage when the unit tests are run in the project.
48+
Now, when you run `ng test`, the tool will throw an error if the coverage drops below 80%.
5649

57-
Read more on coverage configuration options in the [karma coverage documentation](https://github.com/karma-runner/karma-coverage/blob/master/docs/configuration.md).
50+
HELPFUL: Read more about coverage configuration options in the [Vitest documentation](https://vitest.dev/config/#coverage-thresholds).

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: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Testing with Karma
2+
3+
While Vitest is the default testing framework for new Angular projects, Karma is still a supported and widely used test runner. This guide provides instructions for testing your Angular application using the Karma and Jasmine testing stack.
4+
5+
## Setting Up and Running Tests
6+
7+
For new projects, you can configure Karma during generation by specifying the `testRunner` option:
8+
9+
<docs-code language="shell">
10+
ng new my-karma-app --test-runner=karma
11+
</docs-code>
12+
13+
If you are adding Karma to an existing project, you must first install the necessary dependencies:
14+
15+
<docs-code-multifile>
16+
<docs-code header="pnpm" language="shell">
17+
pnpm add -D karma karma-chrome-launcher karma-coverage karma-jasmine karma-jasmine-html-reporter jasmine-core @types/jasmine
18+
</docs-code>
19+
<docs-code header="npm" language="shell">
20+
npm install --save-dev karma karma-chrome-launcher karma-coverage karma-jasmine karma-jasmine-html-reporter jasmine-core @types/jasmine
21+
</docs-code>
22+
<docs-code header="yarn" language="shell">
23+
yarn add --dev karma karma-chrome-launcher karma-coverage karma-jasmine karma-jasmine-html-reporter jasmine-core @types/jasmine
24+
</docs-code>
25+
</docs-code-multifile>
26+
27+
After installing the packages, update your `tsconfig.spec.json` to include the Jasmine types in the `compilerOptions`. This ensures that TypeScript recognizes the global testing functions like `describe` and `it`.
28+
29+
<docs-code language="json" title="tsconfig.spec.json">
30+
{
31+
// ...
32+
"compilerOptions": {
33+
// ...
34+
"types": [
35+
"jasmine"
36+
]
37+
},
38+
// ...
39+
}
40+
</docs-code>
41+
42+
Once your project is configured, run the tests using the [`ng test`](cli/test) command:
43+
44+
<docs-code language="shell">
45+
ng test --runner=karma
46+
</docs-code>
47+
48+
The `ng test` command builds the application in *watch mode* and launches the [Karma test runner](https://karma-runner.github.io).
49+
50+
The console output looks like below:
51+
52+
<docs-code language="shell">
53+
54+
02 11 2022 09:08:28.605:INFO [karma-server]: Karma v6.4.1 server started at http://localhost:9876/
55+
02 11 2022 09:08:28.607:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
56+
02 11 2022 09:08:28.620:INFO [launcher]: Starting browser Chrome
57+
02 11 2022 09:08:31.312:INFO [Chrome]: Connected on socket -LaEYvD2R7MdcS0-AAAB with id 31534482
58+
Chrome: Executed 3 of 3 SUCCESS (0.193 secs / 0.172 secs)
59+
TOTAL: 3 SUCCESS
60+
61+
</docs-code>
62+
63+
The test output is displayed in the browser using [Karma Jasmine HTML Reporter](https://github.com/dfederm/karma-jasmine-html-reporter).
64+
65+
<img alt="Jasmine HTML Reporter in the browser" src="assets/images/guide/testing/initial-jasmine-html-reporter.png">
66+
67+
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").
68+
69+
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.
70+
71+
## Configuration
72+
73+
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.
74+
75+
### Customizing Karma Configuration
76+
77+
If you want to customize Karma, you can create a `karma.conf.js` by running the following command:
78+
79+
<docs-code language="shell">
80+
81+
ng generate config karma
82+
83+
</docs-code>
84+
85+
HELPFUL: Read more about Karma configuration in the [Karma configuration guide](http://karma-runner.github.io/6.4/config/configuration-file.html).
86+
87+
### Setting the Test Runner in `angular.json`
88+
89+
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`:
90+
91+
<docs-code language="json" title="angular.json">
92+
{
93+
// ...
94+
"projects": {
95+
"your-project-name": {
96+
// ...
97+
"architect": {
98+
"test": {
99+
"builder": "@angular/build:unit-test",
100+
"options": {
101+
"runner": "karma",
102+
// ... other options
103+
}
104+
}
105+
}
106+
}
107+
}
108+
}
109+
</docs-code>
110+
111+
## Code coverage enforcement
112+
113+
To enforce a minimum code coverage level, you can use the `check` property in the `coverageReporter` section of your `karma.conf.js` file.
114+
115+
For example, to require a minimum of 80% coverage:
116+
117+
<docs-code language="javascript">
118+
coverageReporter: {
119+
dir: require('path').join(__dirname, './coverage/<project-name>'),
120+
subdir: '.',
121+
reporters: [
122+
{ type: 'html' },
123+
{ type: 'text-summary' }
124+
],
125+
check: {
126+
global: {
127+
statements: 80,
128+
branches: 80,
129+
functions: 80,
130+
lines: 80
131+
}
132+
}
133+
}
134+
</docs-code>
135+
136+
This will cause the test run to fail if the specified coverage thresholds are not met.
137+
138+
Read more on coverage configuration options in the [karma coverage documentation](https://github.com/karma-runner/karma-coverage/blob/master/docs/configuration.md).
139+
140+
## Testing in continuous integration
141+
142+
To run your Karma tests in a CI environment, use the following command:
143+
144+
<docs-code language="shell">
145+
ng test --no-watch --no-progress --browsers=ChromeHeadless
146+
</docs-code>

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

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
Testing your Angular application helps you check that your application is working as you expect.
44

5+
NOTE: While Vitest is the default test runner, Karma is still fully supported. For information on testing with Karma, see the [Karma testing guide](guide/testing/karma).
6+
57
## Set up testing
68

7-
The Angular CLI downloads and installs everything you need to test an Angular application with [Jasmine testing framework](https://jasmine.github.io).
9+
The Angular CLI downloads and installs everything you need to test an Angular application with the [Vitest testing framework](https://vitest.dev).
810

911
The project you create with the CLI is immediately ready to test.
1012
Just run the [`ng test`](cli/test) CLI command:
@@ -15,48 +17,43 @@ ng test
1517

1618
</docs-code>
1719

18-
The `ng test` command builds the application in *watch mode*,
19-
and launches the [Karma test runner](https://karma-runner.github.io).
20+
The `ng test` command builds the application in *watch mode* and launches the [Vitest test runner](https://vitest.dev).
2021

2122
The console output looks like below:
2223

2324
<docs-code language="shell">
2425

25-
02 11 2022 09:08:28.605:INFO [karma-server]: Karma v6.4.1 server started at http://localhost:9876/
26-
02 11 2022 09:08:28.607:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
27-
02 11 2022 09:08:28.620:INFO [launcher]: Starting browser Chrome
28-
02 11 2022 09:08:31.312:INFO [Chrome]: Connected on socket -LaEYvD2R7MdcS0-AAAB with id 31534482
29-
Chrome: Executed 3 of 3 SUCCESS (0.193 secs / 0.172 secs)
30-
TOTAL: 3 SUCCESS
26+
✓ src/app/app.component.spec.ts (3)
27+
✓ AppComponent should create the app
28+
✓ AppComponent should have as title 'my-app'
29+
✓ AppComponent should render title
30+
Test Files 1 passed (1)
31+
Tests 3 passed (3)
32+
Start at 18:18:01
33+
Duration 2.46s (transform 615ms, setup 2ms, collect 2.21s, tests 5ms)
3134

3235
</docs-code>
3336

34-
The last line of the log shows that Karma ran three tests that all passed.
35-
36-
The test output is displayed in the browser using [Karma Jasmine HTML Reporter](https://github.com/dfederm/karma-jasmine-html-reporter).
37-
38-
<img alt="Jasmine HTML Reporter in the browser" src="assets/images/guide/testing/initial-jasmine-html-reporter.png">
39-
40-
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"\).
41-
4237
Meanwhile, the `ng test` command is watching for changes.
4338

44-
To see this in action, make a small change to `app.component.ts` and save.
45-
The tests run again, the browser refreshes, and the new test results appear.
39+
To see this in action, make a small change to `app.ts` and save.
40+
The tests run again, and the new test results appear in the console.
4641

4742
## Configuration
4843

49-
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.
44+
The Angular CLI takes care of the Vitest configuration for you. It constructs the full configuration in memory, based on options specified in the `angular.json` file.
5045

51-
If you want to customize Karma, you can create a `karma.conf.js` by running the following command:
46+
If you want to customize Vitest, you can create a `vitest-base.config.ts` by running the following command:
5247

5348
<docs-code language="shell">
5449

55-
ng generate config karma
50+
ng generate config vitest
5651

5752
</docs-code>
5853

59-
HELPFUL: Read more about Karma configuration in the [Karma configuration guide](http://karma-runner.github.io/6.4/config/configuration-file.html).
54+
IMPORTANT: Using a custom `vitest-base.config.ts` provides powerful customization options. However, the Angular team does not provide support for the specific contents of this file or for any third-party plugins used within it.
55+
56+
HELPFUL: Read more about Vitest configuration in the [Vitest configuration guide](https://vitest.dev/config/).
6057

6158
### Other test frameworks
6259

@@ -65,12 +62,12 @@ Each library and runner has its own distinctive installation procedures, configu
6562

6663
### Test file name and location
6764

68-
Inside the `src/app` folder the Angular CLI generated a test file for the `AppComponent` named `app.component.spec.ts`.
65+
Inside the `src/app` folder the Angular CLI generated a test file for the `App` component named `app.spec.ts`.
6966

70-
IMPORTANT: The test file extension **must be `.spec.ts`** so that tooling can identify it as a file with tests \(also known as a *spec* file\).
67+
IMPORTANT: The test file extension **must be `.spec.ts` or `.test.ts`** so that tooling can identify it as a file with tests \(also known as a *spec* file\).
7168

72-
The `app.component.ts` and `app.component.spec.ts` files are siblings in the same folder.
73-
The root file names \(`app.component`\) are the same for both files.
69+
The `app.ts` and `app.spec.ts` files are siblings in the same folder.
70+
The root file names \(`app`\) are the same for both files.
7471

7572
Adopt these two conventions in your own projects for *every kind* of test file.
7673

@@ -106,9 +103,11 @@ Continuous integration \(CI\) servers let you set up your project repository so
106103
To test your Angular CLI application in Continuous integration \(CI\) run the following command:
107104

108105
<docs-code language="shell">
109-
ng test --no-watch --no-progress --browsers=ChromeHeadless
106+
ng test --no-watch --no-progress
110107
</docs-code>
111108

109+
NOTE: The `--no-watch` flag is often unnecessary in CI environments, as they typically don't use a TTY, which automatically disables watch mode.
110+
112111
## More information on testing
113112

114113
After you've set up your application for testing, you might find the following testing guides useful.

adev/src/content/guide/testing/utility-apis.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Here are the most important static methods, in order of likely utility.
5959

6060
| Methods | Details |
6161
| :----------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
62-
| `configureTestingModule` | The testing shims \(`karma-test-shim`, `browser-test-shim`\) establish the [initial test environment](guide/testing) and a default testing module. The default testing module is configured with basic declaratives and some Angular service substitutes that every tester needs. <br /> Call `configureTestingModule` to refine the testing module configuration for a particular set of tests by adding and removing imports, declarations \(of components, directives, and pipes\), and providers. |
62+
| `configureTestingModule` | The testing shims establish the [initial test environment](guide/testing) and a default testing module. The default testing module is configured with basic declaratives and some Angular service substitutes that every tester needs. <br /> Call `configureTestingModule` to refine the testing module configuration for a particular set of tests by adding and removing imports, declarations \(of components, directives, and pipes\), and providers. |
6363
| `compileComponents` | Compile the testing module asynchronously after you've finished configuring it. You **must** call this method if _any_ of the testing module components have a `templateUrl` or `styleUrls` because fetching component template and style files is necessarily asynchronous. See [compileComponents](guide/testing/components-scenarios#calling-compilecomponents). <br /> After calling `compileComponents`, the `TestBed` configuration is frozen for the duration of the current spec. |
6464
| `createComponent<T>` | Create an instance of a component of type `T` based on the current `TestBed` configuration. After calling `createComponent`, the `TestBed` configuration is frozen for the duration of the current spec. |
6565
| `overrideModule` | Replace metadata for the given `NgModule`. Recall that modules can import other modules. The `overrideModule` method can reach deeply into the current testing module to modify one of these inner modules. |
@@ -70,7 +70,7 @@ Here are the most important static methods, in order of likely utility.
7070
|
7171
`inject` | Retrieve a service from the current `TestBed` injector. The `inject` function is often adequate for this purpose. But `inject` throws an error if it can't provide the service. <br /> What if the service is optional? <br /> The `TestBed.inject()` method takes an optional second parameter, the object to return if Angular can't find the provider \(`null` in this example\): <docs-code header="app/demo/demo.testbed.spec.ts" path="adev/src/content/examples/testing/src/app/demo/demo.testbed.spec.ts" visibleRegion="testbed-get-w-null"/> After calling `TestBed.inject`, the `TestBed` configuration is frozen for the duration of the current spec. |
7272
|
73-
`initTestEnvironment` | Initialize the testing environment for the entire test run. <br /> The testing shims \(`karma-test-shim`, `browser-test-shim`\) call it for you so there is rarely a reason for you to call it yourself. <br /> Call this method _exactly once_. To change this default in the middle of a test run, call `resetTestEnvironment` first. <br /> Specify the Angular compiler factory, a `PlatformRef`, and a default Angular testing module. Alternatives for non-browser platforms are available in the general form `@angular/platform-<platform_name>/testing/<platform_name>`. |
73+
`initTestEnvironment` | Initialize the testing environment for the entire test run. <br /> The testing shims call it for you so there is rarely a reason for you to call it yourself. <br /> Call this method _exactly once_. To change this default in the middle of a test run, call `resetTestEnvironment` first. <br /> Specify the Angular compiler factory, a `PlatformRef`, and a default Angular testing module. Alternatives for non-browser platforms are available in the general form `@angular/platform-<platform_name>/testing/<platform_name>`. |
7474
| `resetTestEnvironment` | Reset the initial test environment, including the default testing module. |
7575

7676
A few of the `TestBed` instance methods are not covered by static `TestBed` _class_ methods.

0 commit comments

Comments
 (0)