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
With the Angular CLI we can run unit tests as well as create code coverage reports. Code coverage reports allow us to see any parts of our code base that may not be properly tested by our unit tests.
4
+
5
+
To generate a coverage report run the following command in the root of your project
6
+
7
+
```bash
8
+
ng test --watch=false --code-coverage
9
+
```
10
+
11
+
Once the tests complete a new `/coverage` folder will appear in the project. In your Finder or Windows Explorer open the `index.html` file. You should see a report with your source code and code coverage values.
12
+
13
+
Using the code coverage percentages we can estimate how much of our code is tested. It is up to your team to determine how much code should be unit tested.
14
+
15
+
## Code Coverage Enforcement
16
+
17
+
If your team decides on a set minimum amount to be unit tested you can enforce this minimum with the Angular CLI. For example our team would like the code base to have a minimum of 80% code coverage. To enable this open the `karma.conf.js` and add the following in the `coverageIstanbulReporter:` key
18
+
19
+
```javascript
20
+
coverageIstanbulReporter: {
21
+
reports: [ 'html', 'lcovonly' ],
22
+
fixWebpackSourcePaths:true,
23
+
thresholds: {
24
+
statements:80,
25
+
lines:80,
26
+
branches:80,
27
+
functions:80
28
+
}
29
+
}
30
+
```
31
+
32
+
The `thresholds` property will enforce a minimum of 80% code coverage when the unit tests are run in the project.
0 commit comments