Skip to content

feat(@angular/cli): expose test include list #4868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/@angular/cli/blueprints/ng2/files/angular-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
"test": {
"karma": {
"config": "./karma.conf.js"
}
},
"include": [
"**/*.spec.ts",
"test.ts"
]
},
"defaults": {
"styleExt": "<%= styleExt %>",
Expand Down
11 changes: 11 additions & 0 deletions packages/@angular/cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@
}
},
"additionalProperties": false
},
"include":{
"description": "Test files to include in the TypeScript compilation.",
"type": "array",
"items": {
"type": "string"
},
"default": [
"**/*.spec.ts",
"test.ts"
]
}
},
"additionalProperties": false
Expand Down
24 changes: 12 additions & 12 deletions packages/@angular/cli/models/webpack-configs/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as fs from 'fs';
import * as path from 'path';
import { stripIndent } from 'common-tags';
import {AotPlugin, AotPluginOptions} from '@ngtools/webpack';

import { CliConfig } from '../config';
import { WebpackConfigOptions } from '../webpack-config';

const SilentError = require('silent-error');
Expand All @@ -13,9 +15,16 @@ const webpackLoader: string = g['angularCliIsLocal']
: '@ngtools/webpack';


function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
function _createAotPlugin(wco: WebpackConfigOptions, options: any = {}) {
const { appConfig, projectRoot, buildOptions } = wco;

// Exclude test files by default.
const testConfig = CliConfig.fromProject().config.test;
options.exclude = (testConfig && testConfig.include) || [
'**/*.spec.ts',
'test.ts'
];

// Read the environment, and set it in the compiler host.
let hostReplacementPaths: any = {};
// process environment file replacement
Expand Down Expand Up @@ -76,12 +85,6 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {


export const getNonAotConfig = function(wco: WebpackConfigOptions) {
const { projectRoot, appConfig } = wco;
let exclude = [ '**/*.spec.ts' ];
if (appConfig.test) {
exclude.push(path.join(projectRoot, appConfig.root, appConfig.test));
}

return {
module: {
rules: [
Expand All @@ -93,15 +96,12 @@ export const getNonAotConfig = function(wco: WebpackConfigOptions) {
]
},
plugins: [
_createAotPlugin(wco, { exclude, skipCodeGeneration: true }),
_createAotPlugin(wco, { skipCodeGeneration: true }),
]
};
};

export const getAotConfig = function(wco: WebpackConfigOptions) {
const { projectRoot, appConfig } = wco;
let exclude = [ '**/*.spec.ts' ];
if (appConfig.test) { exclude.push(path.join(projectRoot, appConfig.root, appConfig.test)); };
return {
module: {
rules: [
Expand All @@ -113,7 +113,7 @@ export const getAotConfig = function(wco: WebpackConfigOptions) {
]
},
plugins: [
_createAotPlugin(wco, { exclude })
_createAotPlugin(wco)
]
};
};
45 changes: 19 additions & 26 deletions packages/@ngtools/webpack/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,35 +113,28 @@ export class AotPlugin implements Tapable {
} catch (err) {
throw new Error(`An error happened while parsing ${this._tsConfigPath} JSON: ${err}.`);
}

// Default excludes to **/*.spec.ts files.
if (!options.hasOwnProperty('exclude')) {
options['exclude'] = ['**/*.spec.ts'];
}

// If the tsconfig doesn't contain any excludes, we must add the default ones before adding
// any extra ones (otherwise we'd include all of these which can cause unexpected errors).
// This is the same logic as present in TypeScript.
if (!tsConfigJson.exclude) {
tsConfigJson['exclude'] = ['node_modules', 'bower_components', 'jspm_packages'];
if (tsConfigJson.compilerOptions && tsConfigJson.compilerOptions.outDir) {
tsConfigJson.exclude.push(tsConfigJson.compilerOptions.outDir);
}
}

// Join our custom excludes with the existing ones.
tsConfigJson.exclude = tsConfigJson.exclude.concat(options.exclude);

const tsConfig = ts.parseJsonConfigFileContent(
tsConfigJson, ts.sys, basePath, null, this._tsConfigPath);

let fileNames = tsConfig.fileNames;
if (options.hasOwnProperty('exclude')) {
let exclude: string[] = typeof options.exclude == 'string'
? [options.exclude as string] : (options.exclude as string[]);

exclude.forEach((pattern: string) => {
const basePathPattern = '(' + basePath.replace(/\\/g, '/')
.replace(/[\-\[\]\/{}()+?.\\^$|*]/g, '\\$&') + ')?';
pattern = pattern
// Replace windows path separators with forward slashes.
.replace(/\\/g, '/')
// Escape characters that are used normally in regexes, except stars.
.replace(/[\-\[\]{}()+?.\\^$|]/g, '\\$&')
// Two stars replacement.
.replace(/\*\*/g, '(?:.*)')
// One star replacement.
.replace(/\*/g, '(?:[^/]*)')
// Escape characters from the basePath and make sure it's forward slashes.
.replace(/^/, basePathPattern);

const re = new RegExp('^' + pattern + '$');
fileNames = fileNames.filter(x => !x.replace(/\\/g, '/').match(re));
});
} else {
fileNames = fileNames.filter(fileName => !/\.spec\.ts$/.test(fileName));
}
this._rootFilePath = fileNames;

// Check the genDir. We generate a default gendir that's under basepath; it will generate
Expand Down
17 changes: 17 additions & 0 deletions tests/e2e/tests/build/aot/exclude.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ng } from '../../../utils/process';
import { writeFile } from '../../../utils/fs';
import { updateJsonFile } from '../../../utils/project';

export default function () {
// Check if **/*.spec.ts files are excluded by default.
return Promise.resolve()
.then(() => updateJsonFile('.angular-cli.json', configJson => {
const test = configJson['test'];
delete test['include'];
}))
// This import would cause aot to fail.
.then(() => writeFile('src/app.component.spec.ts', `
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
`))
.then(() => ng('build', '--aot'));
}