Skip to content

Commit

Permalink
feat(test): make code coverage and lint optional
Browse files Browse the repository at this point in the history
Heavily inspired by @abner's work on angular#1799.

Partially address angular#1980
Close angular#1799
  • Loading branch information
filipesilva committed Oct 23, 2016
1 parent eb63fda commit 0f4efb0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 36 deletions.
2 changes: 1 addition & 1 deletion packages/angular-cli/blueprints/ng2/files/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = function (config) {
config: './angular-cli.json',
environment: 'dev'
},
reporters: ['progress', 'karma-remap-istanbul'],
reporters: config.angularCli.codeCoverage ? ['progress', 'karma-remap-istanbul'] : ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
Expand Down
2 changes: 2 additions & 0 deletions packages/angular-cli/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {CliConfig} from '../models/config';
const NgCliTestCommand = TestCommand.extend({
availableOptions: [
{ name: 'watch', type: Boolean, default: true, aliases: ['w'] },
{ name: 'code-coverage', type: Boolean, default: false, aliases: ['cc'] },
{ name: 'lint', type: Boolean, default: false, aliases: ['l'] },
{ name: 'browsers', type: String },
{ name: 'colors', type: Boolean },
{ name: 'log-level', type: String },
Expand Down
71 changes: 39 additions & 32 deletions packages/angular-cli/models/webpack-build-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,43 @@ const path = require('path');
const webpack = require('webpack');
const atl = require('awesome-typescript-loader');

const getWebpackTestConfig = function (projectRoot, environment, appConfig) {
const getWebpackTestConfig = function (projectRoot, environment, appConfig, testConfig) {

const appRoot = path.resolve(projectRoot, appConfig.root);
const extraRules = [];
const extraPlugins = [];

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

if (testConfig.lint) {
extraRules.push({
test: /\.ts$/,
enforce: 'pre',
loader: 'tslint-loader',
exclude: [
path.resolve(projectRoot, 'node_modules')
]
});
extraPlugins.push(new webpack.LoaderOptionsPlugin({
options: {
tslint: {
emitErrors: false,
failOnHint: false,
resourcePath: `./${appConfig.root}`
}
}
}))
}

return {
devtool: 'inline-source-map',
Expand All @@ -28,21 +62,12 @@ const getWebpackTestConfig = function (projectRoot, environment, appConfig) {
},
module: {
rules: [
{
test: /\.ts$/,
enforce: 'pre',
loader: 'tslint-loader',
exclude: [
path.resolve(projectRoot, 'node_modules')
]
},
{
test: /\.js$/,
enforce: 'pre',
loader: 'source-map-loader',
exclude: [
path.resolve(projectRoot, 'node_modules/rxjs'),
path.resolve(projectRoot, 'node_modules/@angular')
/node_modules/
]
},
{
Expand All @@ -63,23 +88,14 @@ const getWebpackTestConfig = function (projectRoot, environment, appConfig) {
],
exclude: [/\.e2e\.ts$/]
},
{
test: /\.(js|ts)$/, loader: 'sourcemap-istanbul-instrumenter-loader',
enforce: 'post',
exclude: [
/\.(e2e|spec)\.ts$/,
/node_modules/
],
query: { 'force-sourcemap': true }
},
{ test: /\.json$/, loader: 'json-loader' },
{ test: /\.css$/, loaders: ['raw-loader', 'postcss-loader'] },
{ test: /\.css$/, loaders: ['raw-loader', 'postcss-loader'] },
{ test: /\.styl$/, loaders: ['raw-loader', 'postcss-loader', 'stylus-loader'] },
{ test: /\.less$/, loaders: ['raw-loader', 'postcss-loader', 'less-loader'] },
{ test: /\.scss$|\.sass$/, loaders: ['raw-loader', 'postcss-loader', 'sass-loader'] },
{ test: /\.(jpg|png)$/, loader: 'url-loader?limit=128000' },
{ test: /\.html$/, loader: 'raw-loader', exclude: [path.resolve(appRoot, appConfig.index)] }
]
].concat(extraRules)
},
plugins: [
new webpack.SourceMapDevToolPlugin({
Expand All @@ -94,20 +110,11 @@ const getWebpackTestConfig = function (projectRoot, environment, appConfig) {
.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')),
path.resolve(appRoot, appConfig.environments[environment])
),
new webpack.LoaderOptionsPlugin({
options: {
tslint: {
emitErrors: false,
failOnHint: false,
resourcePath: `./${appConfig.root}`
}
}
}),
new webpack.ContextReplacementPlugin(
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
appRoot
)
],
].concat(extraPlugins),
node: {
fs: 'empty',
global: true,
Expand Down
7 changes: 5 additions & 2 deletions packages/angular-cli/plugins/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ const getWebpackTestConfig = require('../models/webpack-build-test').getWebpackT
const CliConfig = require('../models/config').CliConfig;

const init = (config) => {

// load Angular CLI config
if (!config.angularCli || !config.angularCli.config) {
throw new Error('Missing \'angularCli.config\' entry in Karma config');
}
const angularCliConfig = require(path.join(config.basePath, config.angularCli.config));
const appConfig = angularCliConfig.apps[0];
const environment = config.angularCli.environment || 'dev';
const testConfig = {
codeCoverage: config.angularCli.codeCoverage || false,
lint: config.angularCli.lint || false,
}

// add webpack config
const webpackConfig = getWebpackTestConfig(config.basePath, environment, appConfig);
const webpackConfig = getWebpackTestConfig(config.basePath, environment, appConfig, testConfig);
const webpackMiddlewareConfig = {
noInfo: true, // Hide webpack output because its noisy.
stats: { // Also prevent chunk and module display output, cleaner look. Only emit errors.
Expand Down
5 changes: 5 additions & 0 deletions packages/angular-cli/tasks/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export default Task.extend({
options.browsers = options.browsers.split(',');
}

options.angularCli = {
codeCoverage: options.codeCoverage,
lint: options.lint,
};

// Assign additional karmaConfig options to the local ngapp config
options.configFile = karmaConfig;

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/tests/misc/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {ng} from '../../utils/process';


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

0 comments on commit 0f4efb0

Please sign in to comment.