Skip to content

Commit

Permalink
feat(@angular/cli): allow code coverage excludes (#4966)
Browse files Browse the repository at this point in the history
A new CLI config entry under `test` allows you to list exclude globs for code coverage:

```
  "test": {
    "codeCoverage": {
      "exclude": [
        "src/polyfills.ts",
        "**/test.ts"
      ]
    },
    "karma": {
      "config": "./karma.conf.js"
    }
  },
```
  • Loading branch information
filipesilva authored Feb 23, 2017
1 parent 00f21d3 commit b6893d0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
22 changes: 18 additions & 4 deletions packages/@angular/cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"name": {
"type": "string",
"description": "Name of the app."
},
},
"root": {
"type": "string",
"description": "The root directory of the app."
Expand Down Expand Up @@ -270,6 +270,20 @@
}
},
"additionalProperties": false
},
"codeCoverage": {
"type": "object",
"properties": {
"exclude": {
"description": "Globs to exclude from code coverage.",
"type": "array",
"items": {
"type": "string"
},
"default": []
}
},
"additionalProperties": false
}
},
"additionalProperties": false
Expand Down Expand Up @@ -435,19 +449,19 @@
"description": "The host the application will be served on",
"type": "string",
"default": "localhost"

},
"ssl": {
"description": "Enables ssl for the application",
"type": "boolean",
"default": false

},
"sslKey": {
"description": "The ssl key used by the server",
"type": "string",
"default": "ssl/server.key"

},
"sslCert": {
"description": "The ssl certificate used by the server",
Expand Down
24 changes: 19 additions & 5 deletions packages/@angular/cli/models/webpack-configs/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from 'path';
import * as glob from 'glob';
import * as webpack from 'webpack';

import { CliConfig } from '../config';
Expand All @@ -20,14 +21,27 @@ export function getTestConfig(testConfig: WebpackTestOptions) {
const appConfig = CliConfig.fromProject().config.apps[0];
const extraRules: any[] = [];

if (testConfig.codeCoverage) {
if (testConfig.codeCoverage && CliConfig.fromProject()) {
const codeCoverageExclude = CliConfig.fromProject().get('test.codeCoverage.exclude');
let exclude: (string | RegExp)[] = [
/\.(e2e|spec)\.ts$/,
/node_modules/
];

if (codeCoverageExclude) {
codeCoverageExclude.forEach((excludeGlob: string) => {
const excludeFiles = glob
.sync(path.join(projectRoot, excludeGlob), { nodir: true })
.map(file => path.normalize(file));
exclude.push(...excludeFiles);
});
}


extraRules.push({
test: /\.(js|ts)$/, loader: 'istanbul-instrumenter-loader',
enforce: 'post',
exclude: [
/\.(e2e|spec)\.ts$/,
/node_modules/
]
exclude
});
}

Expand Down
23 changes: 20 additions & 3 deletions tests/e2e/tests/misc/coverage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import {expectFileToExist} from '../../utils/fs';
import {expectFileToExist, expectFileToMatch} from '../../utils/fs';
import {updateJsonFile} from '../../utils/project';
import {expectToFail} from '../../utils/utils';
import {ng} from '../../utils/process';


export default function() {
export default function () {
return ng('test', '--single-run', '--code-coverage')
.then(() => expectFileToExist('coverage/src/app'))
.then(() => expectFileToExist('coverage/lcov.info'));
.then(() => expectFileToExist('coverage/lcov.info'))
// Verify code coverage exclude work
.then(() => expectFileToMatch('coverage/lcov.info', 'polyfills.ts'))
.then(() => expectFileToMatch('coverage/lcov.info', 'test.ts'))
.then(() => updateJsonFile('.angular-cli.json', configJson => {
const test = configJson['test'];
test['codeCoverage'] = {
exclude: [
'src/polyfills.ts',
'**/test.ts'
]
};
}))
.then(() => ng('test', '--single-run', '--code-coverage'))
.then(() => expectToFail(() => expectFileToMatch('coverage/lcov.info', 'polyfills.ts')))
.then(() => expectToFail(() => expectFileToMatch('coverage/lcov.info', 'test.ts')));
}

0 comments on commit b6893d0

Please sign in to comment.