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/**/*"],
Answer from Greg on Stack OverflowVideos
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.
I discovered that Jasmine allows you to prefix describe and it methods with an f (for focus): fdescribe and fit. If you use either of these, Karma will only run the relevant tests. To focus the current file, you can just take the top level describe and change it to fdescribe. If you use Jasmine prior to version 2.1, the focusing keywords are: iit and ddescribe.
This example code runs just the first test:
// Jasmine versions >/=2.1 use 'fdescribe'; versions <2.1 use 'ddescribe'
fdescribe('MySpec1', function () {
it('should do something', function () {
// ...
});
});
describe('MyOtherSpec', function () {
it('should do something else', function () {
// ...
});
});
Here is the Jasmine documentation on Focusing Specs, and here is a related SO article that provides additional thoughtful solutions.
This can be achieved these days via the include option.
https://angular.io/cli/test#options
It's a glob match, so as an example:
ng test --include='**/someFolder/*.spec.ts'
I can't find it in the 8.1.0 release notes, but @Swoox mentions below this is a feature after cli version 8.1.0. Thanks for figuring that out.