Skip to content

Commit 70a3e7e

Browse files
committed
feat(test): make code coverage and lint optional
Heavily inspired by @abner's work on angular#1799. Partially address angular#1980 Close angular#1799
1 parent eb63fda commit 70a3e7e

File tree

6 files changed

+55
-36
lines changed

6 files changed

+55
-36
lines changed

packages/angular-cli/blueprints/ng2/files/karma.conf.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ module.exports = function (config) {
2727
config: './angular-cli.json',
2828
environment: 'dev'
2929
},
30-
reporters: ['progress', 'karma-remap-istanbul'],
30+
reporters: config.angularCli && config.angularCli.codeCoverage
31+
? ['progress', 'karma-remap-istanbul']
32+
: ['progress'],
3133
port: 9876,
3234
colors: true,
3335
logLevel: config.LOG_INFO,

packages/angular-cli/commands/test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {CliConfig} from '../models/config';
55
const NgCliTestCommand = TestCommand.extend({
66
availableOptions: [
77
{ name: 'watch', type: Boolean, default: true, aliases: ['w'] },
8+
{ name: 'code-coverage', type: Boolean, default: false, aliases: ['cc'] },
9+
{ name: 'lint', type: Boolean, default: false, aliases: ['l'] },
810
{ name: 'browsers', type: String },
911
{ name: 'colors', type: Boolean },
1012
{ name: 'log-level', type: String },

packages/angular-cli/models/webpack-build-test.js

+39-32
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,43 @@ const path = require('path');
44
const webpack = require('webpack');
55
const atl = require('awesome-typescript-loader');
66

7-
const getWebpackTestConfig = function (projectRoot, environment, appConfig) {
7+
const getWebpackTestConfig = function (projectRoot, environment, appConfig, testConfig) {
88

99
const appRoot = path.resolve(projectRoot, appConfig.root);
10+
const extraRules = [];
11+
const extraPlugins = [];
12+
13+
if (testConfig.codeCoverage) {
14+
extraRules.push({
15+
test: /\.(js|ts)$/, loader: 'sourcemap-istanbul-instrumenter-loader',
16+
enforce: 'post',
17+
exclude: [
18+
/\.(e2e|spec)\.ts$/,
19+
/node_modules/
20+
],
21+
query: { 'force-sourcemap': true }
22+
});
23+
}
24+
25+
if (testConfig.lint) {
26+
extraRules.push({
27+
test: /\.ts$/,
28+
enforce: 'pre',
29+
loader: 'tslint-loader',
30+
exclude: [
31+
path.resolve(projectRoot, 'node_modules')
32+
]
33+
});
34+
extraPlugins.push(new webpack.LoaderOptionsPlugin({
35+
options: {
36+
tslint: {
37+
emitErrors: false,
38+
failOnHint: false,
39+
resourcePath: `./${appConfig.root}`
40+
}
41+
}
42+
}))
43+
}
1044

1145
return {
1246
devtool: 'inline-source-map',
@@ -28,21 +62,12 @@ const getWebpackTestConfig = function (projectRoot, environment, appConfig) {
2862
},
2963
module: {
3064
rules: [
31-
{
32-
test: /\.ts$/,
33-
enforce: 'pre',
34-
loader: 'tslint-loader',
35-
exclude: [
36-
path.resolve(projectRoot, 'node_modules')
37-
]
38-
},
3965
{
4066
test: /\.js$/,
4167
enforce: 'pre',
4268
loader: 'source-map-loader',
4369
exclude: [
44-
path.resolve(projectRoot, 'node_modules/rxjs'),
45-
path.resolve(projectRoot, 'node_modules/@angular')
70+
/node_modules/
4671
]
4772
},
4873
{
@@ -63,23 +88,14 @@ const getWebpackTestConfig = function (projectRoot, environment, appConfig) {
6388
],
6489
exclude: [/\.e2e\.ts$/]
6590
},
66-
{
67-
test: /\.(js|ts)$/, loader: 'sourcemap-istanbul-instrumenter-loader',
68-
enforce: 'post',
69-
exclude: [
70-
/\.(e2e|spec)\.ts$/,
71-
/node_modules/
72-
],
73-
query: { 'force-sourcemap': true }
74-
},
7591
{ test: /\.json$/, loader: 'json-loader' },
76-
{ test: /\.css$/, loaders: ['raw-loader', 'postcss-loader'] },
92+
{ test: /\.css$/, loaders: ['raw-loader', 'postcss-loader'] },
7793
{ test: /\.styl$/, loaders: ['raw-loader', 'postcss-loader', 'stylus-loader'] },
7894
{ test: /\.less$/, loaders: ['raw-loader', 'postcss-loader', 'less-loader'] },
7995
{ test: /\.scss$|\.sass$/, loaders: ['raw-loader', 'postcss-loader', 'sass-loader'] },
8096
{ test: /\.(jpg|png)$/, loader: 'url-loader?limit=128000' },
8197
{ test: /\.html$/, loader: 'raw-loader', exclude: [path.resolve(appRoot, appConfig.index)] }
82-
]
98+
].concat(extraRules)
8399
},
84100
plugins: [
85101
new webpack.SourceMapDevToolPlugin({
@@ -94,20 +110,11 @@ const getWebpackTestConfig = function (projectRoot, environment, appConfig) {
94110
.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')),
95111
path.resolve(appRoot, appConfig.environments[environment])
96112
),
97-
new webpack.LoaderOptionsPlugin({
98-
options: {
99-
tslint: {
100-
emitErrors: false,
101-
failOnHint: false,
102-
resourcePath: `./${appConfig.root}`
103-
}
104-
}
105-
}),
106113
new webpack.ContextReplacementPlugin(
107114
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
108115
appRoot
109116
)
110-
],
117+
].concat(extraPlugins),
111118
node: {
112119
fs: 'empty',
113120
global: true,

packages/angular-cli/plugins/karma.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ const getWebpackTestConfig = require('../models/webpack-build-test').getWebpackT
33
const CliConfig = require('../models/config').CliConfig;
44

55
const init = (config) => {
6-
76
// load Angular CLI config
87
if (!config.angularCli || !config.angularCli.config) {
98
throw new Error('Missing \'angularCli.config\' entry in Karma config');
109
}
1110
const angularCliConfig = require(path.join(config.basePath, config.angularCli.config));
1211
const appConfig = angularCliConfig.apps[0];
1312
const environment = config.angularCli.environment || 'dev';
13+
const testConfig = {
14+
codeCoverage: config.angularCli.codeCoverage || false,
15+
lint: config.angularCli.lint || false,
16+
}
1417

1518
// add webpack config
16-
const webpackConfig = getWebpackTestConfig(config.basePath, environment, appConfig);
19+
const webpackConfig = getWebpackTestConfig(config.basePath, environment, appConfig, testConfig);
1720
const webpackMiddlewareConfig = {
1821
noInfo: true, // Hide webpack output because its noisy.
1922
stats: { // Also prevent chunk and module display output, cleaner look. Only emit errors.

packages/angular-cli/tasks/test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export default Task.extend({
2020
options.browsers = options.browsers.split(',');
2121
}
2222

23+
options.angularCli = {
24+
codeCoverage: options.codeCoverage,
25+
lint: options.lint,
26+
};
27+
2328
// Assign additional karmaConfig options to the local ngapp config
2429
options.configFile = karmaConfig;
2530

tests/e2e/tests/misc/coverage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {ng} from '../../utils/process';
33

44

55
export default function() {
6-
return ng('test', '--watch=false')
6+
return ng('test', '--watch=false', '--code-coverage')
77
.then(() => expectFileToExist('coverage/src/app'))
88
.then(() => expectFileToExist('coverage/coverage.lcov'));
99
}

0 commit comments

Comments
 (0)