Skip to content

Commit b6893d0

Browse files
authored
feat(@angular/cli): allow code coverage excludes (#4966)
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" } }, ```
1 parent 00f21d3 commit b6893d0

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

packages/@angular/cli/lib/config/schema.json

+18-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"name": {
3333
"type": "string",
3434
"description": "Name of the app."
35-
},
35+
},
3636
"root": {
3737
"type": "string",
3838
"description": "The root directory of the app."
@@ -270,6 +270,20 @@
270270
}
271271
},
272272
"additionalProperties": false
273+
},
274+
"codeCoverage": {
275+
"type": "object",
276+
"properties": {
277+
"exclude": {
278+
"description": "Globs to exclude from code coverage.",
279+
"type": "array",
280+
"items": {
281+
"type": "string"
282+
},
283+
"default": []
284+
}
285+
},
286+
"additionalProperties": false
273287
}
274288
},
275289
"additionalProperties": false
@@ -435,19 +449,19 @@
435449
"description": "The host the application will be served on",
436450
"type": "string",
437451
"default": "localhost"
438-
452+
439453
},
440454
"ssl": {
441455
"description": "Enables ssl for the application",
442456
"type": "boolean",
443457
"default": false
444-
458+
445459
},
446460
"sslKey": {
447461
"description": "The ssl key used by the server",
448462
"type": "string",
449463
"default": "ssl/server.key"
450-
464+
451465
},
452466
"sslCert": {
453467
"description": "The ssl certificate used by the server",

packages/@angular/cli/models/webpack-configs/test.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as path from 'path';
2+
import * as glob from 'glob';
23
import * as webpack from 'webpack';
34

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

23-
if (testConfig.codeCoverage) {
24+
if (testConfig.codeCoverage && CliConfig.fromProject()) {
25+
const codeCoverageExclude = CliConfig.fromProject().get('test.codeCoverage.exclude');
26+
let exclude: (string | RegExp)[] = [
27+
/\.(e2e|spec)\.ts$/,
28+
/node_modules/
29+
];
30+
31+
if (codeCoverageExclude) {
32+
codeCoverageExclude.forEach((excludeGlob: string) => {
33+
const excludeFiles = glob
34+
.sync(path.join(projectRoot, excludeGlob), { nodir: true })
35+
.map(file => path.normalize(file));
36+
exclude.push(...excludeFiles);
37+
});
38+
}
39+
40+
2441
extraRules.push({
2542
test: /\.(js|ts)$/, loader: 'istanbul-instrumenter-loader',
2643
enforce: 'post',
27-
exclude: [
28-
/\.(e2e|spec)\.ts$/,
29-
/node_modules/
30-
]
44+
exclude
3145
});
3246
}
3347

tests/e2e/tests/misc/coverage.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1-
import {expectFileToExist} from '../../utils/fs';
1+
import {expectFileToExist, expectFileToMatch} from '../../utils/fs';
2+
import {updateJsonFile} from '../../utils/project';
3+
import {expectToFail} from '../../utils/utils';
24
import {ng} from '../../utils/process';
35

46

5-
export default function() {
7+
export default function () {
68
return ng('test', '--single-run', '--code-coverage')
79
.then(() => expectFileToExist('coverage/src/app'))
8-
.then(() => expectFileToExist('coverage/lcov.info'));
10+
.then(() => expectFileToExist('coverage/lcov.info'))
11+
// Verify code coverage exclude work
12+
.then(() => expectFileToMatch('coverage/lcov.info', 'polyfills.ts'))
13+
.then(() => expectFileToMatch('coverage/lcov.info', 'test.ts'))
14+
.then(() => updateJsonFile('.angular-cli.json', configJson => {
15+
const test = configJson['test'];
16+
test['codeCoverage'] = {
17+
exclude: [
18+
'src/polyfills.ts',
19+
'**/test.ts'
20+
]
21+
};
22+
}))
23+
.then(() => ng('test', '--single-run', '--code-coverage'))
24+
.then(() => expectToFail(() => expectFileToMatch('coverage/lcov.info', 'polyfills.ts')))
25+
.then(() => expectToFail(() => expectFileToMatch('coverage/lcov.info', 'test.ts')));
926
}

0 commit comments

Comments
 (0)