Videos
With the latest CLI, inside angular.json
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "./karma.conf.js",
"codeCoverageExclude": ["src/testing/**/*"],
Updated September 2019
With Angular CLI 6, angular-cli.json has been renamed to angular.json which contains the configuration. In angular.json, codeCoverage expects a boolean value, which sets whether code-coverage should be done with every test run or not. To exclude files from code coverage, there is a property codeCoverageExclude which accepts an array of files to be excluded from code coverage.
angular.json
"test": {
"options": {
"codeCoverageExclude": ["src/assets/vendor/**"],
...
},
...
}
Updated answer
rc.0 has been released. You should be able to add the code snippet below to ignore files.
Original answer
Currently, you aren't able to do so in beta.32.3. A change is coming to allow this to happen. In the next release (probably rc.0), you will be able to do the following:
.angular-cli.json
"test": {
"codeCoverage": {
"exclude": [
"src/app/quote/services/generated/**/*"
]
},
...
}
I had the same issue and I found a simple workaround that does the trick for me without any big configuration.
- In your app folder, create a file app.module.spec.ts
- In this file add an import to your app module.
import './app.module';
That's it ;)
The point is, your app module is most likely the central part of your application which imports any other used files directly or indirectly. Now that you have created the spec file, everything that is included by your module should also be included in the test coverage report.
I am not 100% sure if this works with lazy loaded modules. If not, you can simply import those lazy loaded modules in your app.module.spec.ts as well, or create a spec file per module, where you import the module.
Here is the way to do this:
Add
clientsection to yourkarma.conf.jslike this:plugins: [ ... ], client: { codeCoverage: config.angularCli.codeCoverage }, files: [ ... ],Change your
test.tsto require files according tocodeCoverageparameter:let context; if (__karma__.config.codeCoverage) { context = require.context('./app/', true, /\.ts/); } else { context = require.context('./app/', true, /\.spec\.ts/); } context.keys().map(context);
UPDATE:
Since Angular CLI 1.5.0 additional steps are required:
Next to
tsconfig.spec.jsonaddtsconfig-cc.spec.jsonfile with the following content:{ "extends": "./tsconfig.spec.json", "include": [ "**/*.ts" ] }In your
angular-cli.jsonadd the following toappsarray:{ "root": "src/", "polyfills": "polyfills.ts", "test": "test.ts", "testTsconfig": "tsconfig-cc.spec.json" }In your
karma.conf.jsadd the following toangularClisection:app: config.angularCli.codeCoverage ? '1' : '0'eventually it should look something like this:
angularCli: { environment: 'dev', app: config.angularCli.codeCoverage ? '1' : '0' },
So what's happening here?
Apparently they have fixed Angular compiler plugin and it takes the file globs from tsconfig.spec.json now. As long as we include only **/*.spec.ts in tsconfig.spec.json these are the only files that will be included in coverage.
The obvious solution is making tsconfig.spec.json include all the files (in addition to require.context). However, this will slow down all the tests even when running without coverage (which we don't want to).
One of the solutions is using the ability of angular-cli to work with multiple apps.
By adding another entry into apps array, we're adding another configuration for "another" (which is actually the same one) app.
We strip out all the irrelevant information in this config, leaving just the test configuration, and put another tsconfig which includes all the ts files.
Finally, we're telling angular-cli karma plugin to run the tests with the configuration of the second app (index 1) in case it is running with code coverage and run with the configuration of the first app (index 0) if it is running without code coverage.
Important note: in this configuration I assume you have only one application in .angular-cli.json. In case you have more you have to adjust indexes accordingly.
If you want to see overall test coverage statistics than of course in Angular CLI you can just type, and see the bottom of your command prompt window
ng test --code-coverage
result:

if you want to see component's individual coverage of tests follow these steps.
npm install --save-dev karma-teamcity-reporterAdd
require('karma-teamcity-reporter')to list of plugins in karma.conf.jsng test --code-coverage --reporters=teamcity,coverage-istanbul
note that list of reporters is comma-separated, as we have added a new reporter, teamcity.
after running this command you can see the folder coverage in your dir and open index.html for a graphical view of test coverage.

You can also set the coverage threshold that you want to achieve, in karma.conf.js, like this.
coverageIstanbulReporter: {
reports: ['html', 'lcovonly'],
fixWebpackSourcePaths: true,
thresholds: {
statements: 90,
lines: 90,
branches: 90,
functions: 90
}
},
First install the dependencies.
npm install karma karma-jasmine karma-chrome-launcher karma-jasmine-html-reporter karma-coverage-istanbul-reporter
Then run ng test.
ng test --code-coverage
Then run the server that shows you your report.
http-server -c-1 -o -p 9875 ./coverage
You should see something like this:

I wrote a blog post about this here.