Skip to content

Commit 0176828

Browse files
committed
feat(@angular/cli): expose test include list
The `karma` entry in `.angular-cli.json` now supports an `include` property, listing the test specific files to be included in the TypeScript compilation: ``` "test": { "karma": { "config": "./karma.conf.js" }, "include": [ "**/*.spec.ts", "test.ts" ] }, ``` Existing projects will default to the list abose. This list will also be used as an exclude list for AoT Compilations. Fix #3973
1 parent a4b43a5 commit 0176828

File tree

4 files changed

+34
-40
lines changed

4 files changed

+34
-40
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@
262262
}
263263
},
264264
"additionalProperties": false
265+
},
266+
"include":{
267+
"description": "Test files to include in the TypeScript compilation.",
268+
"type": "array",
269+
"items": {
270+
"type": "string"
271+
},
272+
"default": [
273+
"**/*.spec.ts",
274+
"test.ts"
275+
]
265276
}
266277
},
267278
"additionalProperties": false

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as fs from 'fs';
22
import * as path from 'path';
33
import { stripIndent } from 'common-tags';
44
import {AotPlugin, AotPluginOptions} from '@ngtools/webpack';
5+
6+
import { CliConfig } from '../config';
57
import { WebpackConfigOptions } from '../webpack-config';
68

79
const SilentError = require('silent-error');
@@ -13,9 +15,12 @@ const webpackLoader: string = g['angularCliIsLocal']
1315
: '@ngtools/webpack';
1416

1517

16-
function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
18+
function _createAotPlugin(wco: WebpackConfigOptions, options: any = {}) {
1719
const { appConfig, projectRoot, buildOptions } = wco;
1820

21+
// Exclude test files by default.
22+
options.exclude = CliConfig.fromProject().config.test.include;
23+
1924
// Read the environment, and set it in the compiler host.
2025
let hostReplacementPaths: any = {};
2126
// process environment file replacement
@@ -76,12 +81,6 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
7681

7782

7883
export const getNonAotConfig = function(wco: WebpackConfigOptions) {
79-
const { projectRoot, appConfig } = wco;
80-
let exclude = [ '**/*.spec.ts' ];
81-
if (appConfig.test) {
82-
exclude.push(path.join(projectRoot, appConfig.root, appConfig.test));
83-
}
84-
8584
return {
8685
module: {
8786
rules: [
@@ -93,15 +92,12 @@ export const getNonAotConfig = function(wco: WebpackConfigOptions) {
9392
]
9493
},
9594
plugins: [
96-
_createAotPlugin(wco, { exclude, skipCodeGeneration: true }),
95+
_createAotPlugin(wco, { skipCodeGeneration: true }),
9796
]
9897
};
9998
};
10099

101100
export const getAotConfig = function(wco: WebpackConfigOptions) {
102-
const { projectRoot, appConfig } = wco;
103-
let exclude = [ '**/*.spec.ts' ];
104-
if (appConfig.test) { exclude.push(path.join(projectRoot, appConfig.root, appConfig.test)); };
105101
return {
106102
module: {
107103
rules: [
@@ -113,7 +109,7 @@ export const getAotConfig = function(wco: WebpackConfigOptions) {
113109
]
114110
},
115111
plugins: [
116-
_createAotPlugin(wco, { exclude })
112+
_createAotPlugin(wco)
117113
]
118114
};
119115
};

packages/@ngtools/webpack/src/plugin.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -113,35 +113,16 @@ export class AotPlugin implements Tapable {
113113
} catch (err) {
114114
throw new Error(`An error happened while parsing ${this._tsConfigPath} JSON: ${err}.`);
115115
}
116+
117+
// Default excludes to **/*.spec.ts files.
118+
if (!options.hasOwnProperty('exclude')) {
119+
options.exclude = ['**/*.spec.ts'];
120+
}
121+
tsConfigJson.exclude = (tsConfigJson.exclude || []).concat(options.exclude);
122+
116123
const tsConfig = ts.parseJsonConfigFileContent(
117124
tsConfigJson, ts.sys, basePath, null, this._tsConfigPath);
118-
119125
let fileNames = tsConfig.fileNames;
120-
if (options.hasOwnProperty('exclude')) {
121-
let exclude: string[] = typeof options.exclude == 'string'
122-
? [options.exclude as string] : (options.exclude as string[]);
123-
124-
exclude.forEach((pattern: string) => {
125-
const basePathPattern = '(' + basePath.replace(/\\/g, '/')
126-
.replace(/[\-\[\]\/{}()+?.\\^$|*]/g, '\\$&') + ')?';
127-
pattern = pattern
128-
// Replace windows path separators with forward slashes.
129-
.replace(/\\/g, '/')
130-
// Escape characters that are used normally in regexes, except stars.
131-
.replace(/[\-\[\]{}()+?.\\^$|]/g, '\\$&')
132-
// Two stars replacement.
133-
.replace(/\*\*/g, '(?:.*)')
134-
// One star replacement.
135-
.replace(/\*/g, '(?:[^/]*)')
136-
// Escape characters from the basePath and make sure it's forward slashes.
137-
.replace(/^/, basePathPattern);
138-
139-
const re = new RegExp('^' + pattern + '$');
140-
fileNames = fileNames.filter(x => !x.replace(/\\/g, '/').match(re));
141-
});
142-
} else {
143-
fileNames = fileNames.filter(fileName => !/\.spec\.ts$/.test(fileName));
144-
}
145126
this._rootFilePath = fileNames;
146127

147128
// Check the genDir. We generate a default gendir that's under basepath; it will generate

tests/e2e/tests/build/aot/aot.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import {ng} from '../../../utils/process';
2-
import {expectFileToMatch} from '../../../utils/fs';
2+
import {expectFileToMatch, writeFile, createDir} from '../../../utils/fs';
33

44
export default function() {
55
return ng('build', '--aot')
66
.then(() => expectFileToMatch('dist/main.bundle.js',
7-
/bootstrapModuleFactory.*\/\* AppModuleNgFactory \*\//));
7+
/bootstrapModuleFactory.*\/\* AppModuleNgFactory \*\//))
8+
// Check if **/*.spec.ts files are excluded by default. This import will cause aot to fail.
9+
.then(() => createDir('./src/much/deep/folder'))
10+
.then(() => writeFile('./src/much/deep/folder/unit-test.spec.ts', `
11+
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
12+
`))
13+
.then(() => ng('build', '--aot'));
814
}

0 commit comments

Comments
 (0)