|
| 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