You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
The test output is displayed in the browser using [Karma Jasmine HTML Reporter](https://github.com/dfederm/karma-jasmine-html-reporter).
103
+
104
+
<imgalt="Jasmine HTML Reporter in the browser"src="assets/images/guide/testing/initial-jasmine-html-reporter.png">
105
+
106
+
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").
107
+
108
+
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.
109
+
110
+
## Configuration
111
+
112
+
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.
113
+
114
+
### Customizing Karma Configuration
115
+
116
+
If you want to customize Karma, you can create a `karma.conf.js` by running the following command:
117
+
118
+
```shell
119
+
ng generate config karma
120
+
```
121
+
122
+
HELPFUL: Read more about Karma configuration in the [Karma configuration guide](http://karma-runner.github.io/6.4/config/configuration-file.html).
123
+
124
+
### Setting the Test Runner in `angular.json`
125
+
126
+
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`:
127
+
128
+
```json
129
+
{
130
+
// ...
131
+
"projects": {
132
+
"your-project-name": {
133
+
// ...
134
+
"architect": {
135
+
"test": {
136
+
"builder": "@angular/build:unit-test",
137
+
"options": {
138
+
"runner": "karma",
139
+
// ... other options
140
+
}
141
+
}
142
+
}
143
+
}
144
+
}
145
+
}
146
+
```
147
+
148
+
## Code coverage enforcement
149
+
150
+
To enforce a minimum code coverage level, you can use the `check` property in the `coverageReporter` section of your `karma.conf.js` file.
151
+
152
+
For example, to require a minimum of 80% coverage:
This will cause the test run to fail if the specified coverage thresholds are not met.
174
+
175
+
## Testing in continuous integration
176
+
177
+
To run your Karma tests in a CI environment, use the following command:
178
+
179
+
```shell
180
+
ng test --no-watch --no-progress --browsers=ChromeHeadless
181
+
```
182
+
183
+
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.
184
+
185
+
## Debugging tests
186
+
187
+
If your tests aren't working as you expect, you can inspect and debug them in the browser.
188
+
189
+
To debug an application with the Karma test runner:
190
+
191
+
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.
192
+
2. Click the **DEBUG** button to open a new browser tab and re-run the tests.
193
+
3. Open the browser's **Developer Tools**. On Windows, press `Ctrl-Shift-I`. On macOS, press `Command-Option-I`.
194
+
4. Pick the **Sources** section.
195
+
5. Press `Control/Command-P`, and then start typing the name of your test file to open it.
196
+
6. Set a breakpoint in the test.
197
+
7. Refresh the browser, and notice how it stops at the breakpoint.
Copy file name to clipboardExpand all lines: adev-ja/src/content/guide/testing/karma.md
+46-46Lines changed: 46 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,24 @@
1
-
# Testing with Karma and Jasmine
1
+
# KarmaとJasmineを使用したテスト
2
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.
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.
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.
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`:
To run your Karma tests in a CI environment, use the following command:
177
+
CI環境でKarmaテストを実行するには、次のコマンドを使用します:
178
178
179
179
```shell
180
180
ng test --no-watch --no-progress --browsers=ChromeHeadless
181
181
```
182
182
183
-
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