forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkarma.ts
275 lines (235 loc) · 9.26 KB
/
karma.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import * as path from 'path';
import * as fs from 'fs';
import * as glob from 'glob';
import * as webpack from 'webpack';
const webpackDevMiddleware = require('webpack-dev-middleware');
import { Pattern } from './glob-copy-webpack-plugin';
import { WebpackTestConfig, WebpackTestOptions } from '../models/webpack-test-config';
import { KarmaWebpackThrowError } from './karma-webpack-throw-error';
/**
* Enumerate needed (but not require/imported) dependencies from this file
* to let the dependency validator know they are used.
*
* require('karma-source-map-support')
*/
const getAppFromConfig = require('../utilities/app-utils').getAppFromConfig;
let blocked: any[] = [];
let isBlocked = false;
function isDirectory(path: string) {
try {
return fs.statSync(path).isDirectory();
} catch (_) {
return false;
}
}
// Add files to the Karma files array.
function addKarmaFiles(files: any[], newFiles: any[], prepend = false) {
const defaults = {
included: true,
served: true,
watched: true
};
const processedFiles = newFiles
// Remove globs that do not match any files, otherwise Karma will show a warning for these.
.filter(file => glob.sync(file.pattern, { nodir: true }).length != 0)
// Fill in pattern properties with defaults.
.map(file => ({ ...defaults, ...file }));
// It's important to not replace the array, because
// karma already has a reference to the existing array.
if (prepend) {
files.unshift(...processedFiles);
} else {
files.push(...processedFiles);
}
}
const init: any = (config: any, emitter: any, customFileHandlers: any) => {
const appConfig = getAppFromConfig(config.angularCli.app);
const appRoot = path.join(config.basePath, appConfig.root);
const testConfig: WebpackTestOptions = Object.assign({
environment: 'dev',
codeCoverage: false,
sourcemaps: true,
progress: true,
}, config.angularCli);
if (testConfig.sourcemaps) {
// Add a reporter that fixes sourcemap urls.
config.reporters.unshift('@angular/cli');
// Code taken from https://github.com/tschaub/karma-source-map-support.
// We can't use it directly because we need to add it conditionally in this file, and karma
// frameworks cannot be added dynamically.
const smsPath = path.dirname(require.resolve('source-map-support'));
const ksmsPath = path.dirname(require.resolve('karma-source-map-support'));
addKarmaFiles(config.files, [
{ pattern: path.join(smsPath, 'browser-source-map-support.js'), watched: false },
{ pattern: path.join(ksmsPath, 'client.js'), watched: false }
], true);
}
// Add assets. This logic is mimics the one present in GlobCopyWebpackPlugin.
if (appConfig.assets) {
config.proxies = config.proxies || {};
appConfig.assets.forEach((pattern: Pattern) => {
// Convert all string patterns to Pattern type.
pattern = typeof pattern === 'string' ? { glob: pattern } : pattern;
// Add defaults.
// Input is always resolved relative to the appRoot.
pattern.input = path.resolve(appRoot, pattern.input || '');
pattern.output = pattern.output || '';
pattern.glob = pattern.glob || '';
// Build karma file pattern.
const assetPath = path.join(pattern.input, pattern.glob);
const filePattern = isDirectory(assetPath) ? assetPath + '/**' : assetPath;
addKarmaFiles(config.files, [{ pattern: filePattern, included: false }]);
// The `files` entry serves the file from `/base/{asset.input}/{asset.glob}`.
// We need to add a URL rewrite that exposes the asset as `/{asset.output}/{asset.glob}`.
let relativePath: string, proxyPath: string;
if (fs.existsSync(assetPath)) {
relativePath = path.relative(config.basePath, assetPath);
proxyPath = path.join(pattern.output, pattern.glob);
} else {
// For globs (paths that don't exist), proxy pattern.output to pattern.input.
relativePath = path.relative(config.basePath, pattern.input);
proxyPath = path.join(pattern.output);
}
// Proxy paths must have only forward slashes.
proxyPath = proxyPath.replace(/\\/g, '/');
config.proxies['/' + proxyPath] = '/base/' + relativePath;
});
}
// Add webpack config.
const webpackConfig = new WebpackTestConfig(testConfig, appConfig).buildConfig();
const webpackMiddlewareConfig = {
noInfo: true, // Hide webpack output because its noisy.
watchOptions: { poll: testConfig.poll },
publicPath: '/_karma_webpack_/',
stats: { // Also prevent chunk and module display output, cleaner look. Only emit errors.
assets: false,
colors: true,
version: false,
hash: false,
timings: false,
chunks: false,
chunkModules: false
}
};
// If Karma is being ran in single run mode, throw errors.
if (config.singleRun) {
webpackConfig.plugins.push(new KarmaWebpackThrowError());
}
// Use existing config if any.
config.webpack = Object.assign(webpackConfig, config.webpack);
config.webpackMiddleware = Object.assign(webpackMiddlewareConfig, config.webpackMiddleware);
// Remove the @angular/cli test file if present, for backwards compatibility.
const testFilePath = path.join(appRoot, appConfig.test);
config.files.forEach((file: any, index: number) => {
if (path.normalize(file.pattern) === testFilePath) {
config.files.splice(index, 1);
}
});
// When using code-coverage, auto-add coverage-istanbul.
config.reporters = config.reporters || [];
if (testConfig.codeCoverage && config.reporters.indexOf('coverage-istanbul') === -1) {
config.reporters.push('coverage-istanbul');
}
// Our custom context and debug files list the webpack bundles directly instead of using
// the karma files array.
config.customContextFile = `${__dirname}/karma-context.html`;
config.customDebugFile = `${__dirname}/karma-debug.html`;
// Add the request blocker.
config.beforeMiddleware = config.beforeMiddleware || [];
config.beforeMiddleware.push('angularCliBlocker');
// Delete global styles entry, we don't want to load them.
delete webpackConfig.entry.styles;
// The webpack tier owns the watch behavior so we want to force it in the config.
webpackConfig.watch = true;
// Files need to be served from a custom path for Karma.
webpackConfig.output.path = '/_karma_webpack_/';
webpackConfig.output.publicPath = '/_karma_webpack_/';
let compiler: any;
try {
compiler = webpack(webpackConfig);
} catch (e) {
console.error(e.stack || e);
if (e.details) {
console.error(e.details);
}
throw e;
}
['invalid', 'watch-run', 'run'].forEach(function (name) {
compiler.plugin(name, function (_: any, callback: () => void) {
isBlocked = true;
if (typeof callback === 'function') {
callback();
}
});
});
compiler.plugin('done', (stats: any) => {
// Don't refresh karma when there are webpack errors.
if (stats.compilation.errors.length === 0) {
emitter.refreshFiles();
isBlocked = false;
blocked.forEach((cb) => cb());
blocked = [];
}
});
const middleware = new webpackDevMiddleware(compiler, webpackMiddlewareConfig);
// Forward requests to webpack server.
customFileHandlers.push({
urlRegex: /^\/_karma_webpack_\/.*/,
handler: function handler(req: any, res: any) {
middleware(req, res, function () {
// Ensure script and style bundles are served.
// They are mentioned in the custom karma context page and we don't want them to 404.
const alwaysServe = [
'/_karma_webpack_/inline.bundle.js',
'/_karma_webpack_/polyfills.bundle.js',
'/_karma_webpack_/scripts.bundle.js',
'/_karma_webpack_/vendor.bundle.js',
];
if (alwaysServe.indexOf(req.url) != -1) {
res.statusCode = 200;
res.end();
} else {
res.statusCode = 404;
res.end('Not found');
}
});
}
});
emitter.on('exit', (done: any) => {
middleware.close();
done();
});
};
init.$inject = ['config', 'emitter', 'customFileHandlers'];
// Dummy preprocessor, just to keep karma from showing a warning.
const preprocessor: any = () => (content: any, _file: string, done: any) => done(null, content);
preprocessor.$inject = [];
// Block requests until the Webpack compilation is done.
function requestBlocker() {
return function (_request: any, _response: any, next: () => void) {
if (isBlocked) {
blocked.push(next);
} else {
next();
}
};
}
// Strip the server address and webpack scheme (webpack://) from error log.
const initSourcemapReporter: any = function (baseReporterDecorator: any) {
baseReporterDecorator(this);
const urlRegexp = /\(http:\/\/localhost:\d+\/_karma_webpack_\/webpack:\//gi;
this.onSpecComplete = function (_browser: any, result: any) {
if (!result.success && result.log.length > 0) {
result.log.forEach((log: string, idx: number) => {
result.log[idx] = log.replace(urlRegexp, '');
});
}
};
};
initSourcemapReporter.$inject = ['baseReporterDecorator'];
module.exports = Object.assign({
'framework:@angular/cli': ['factory', init],
'preprocessor:@angular/cli': ['factory', preprocessor],
'reporter:@angular/cli': ['type', initSourcemapReporter],
'middleware:angularCliBlocker': ['factory', requestBlocker]
});