Skip to content

Commit 455d56e

Browse files
filipesilvaBrocco
authored andcommitted
fix(@angular/cli): fix ng test report sourcemaps
#6160 broke `ng test` sourcemaps in Chrome, this PR fixes it. Before: ``` kamik@T460p MINGW64 D:/sandbox/master-project (master) $ ng test --sr 10% building modules 1/1 modules 0 active13 07 2017 16:38:43.942:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0:9876/ 13 07 2017 16:38:43.945:INFO [launcher]: Launching browser Chrome with unlimited concurrency 13 07 2017 16:38:43.953:INFO [launcher]: Starting browser Chrome 13 07 2017 16:38:54.099:INFO [Chrome 59.0. 3071 (Windows 10 0.0.0)]: Connected on socket Xdep72ofT_Q7MWBHAAAA with id 76010950 Chrome 59.0.3071 (Windows 10 0.0.0) AppComponent should have as title 'app' FAILED Expected 'app' to equal 'forced bug'. at Object.<anonymous> (http://localhost:9876/_karma_webpack_/main.bundle.js:89:27) at ZoneDelegate.webpackJsonp.../../../../../../../sandbox/master-project/node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9 876/_karma_webpack_/polyfills.bundle.js:2704:26) at AsyncTestZoneSpec.webpackJsonp.../../../../../../../sandbox/master-project/node_modules/zone.js/dist/async-test.js.AsyncTestZoneSpec.onInvoke ( http://localhost:9876/_karma_webpack_/vendor.bundle.js:55972:39) at ProxyZoneSpec.webpackJsonp.../../../../../../../sandbox/master-project/node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (http://localh ost:9876/_karma_webpack_/vendor.bundle.js:56735:39) Chrome 59.0.3071 (Windows 10 0.0.0): Executed 2 of 3 (1 FAILED) (0 secs / 0.162 secs) Chrome 59.0.3071 (Windows 10 0.0.0) AppComponent should have as title 'app' FAILED Expected 'app' to equal 'forced bug'. at Object.<anonymous> (http://localhost:9876/_karma_webpack_/main.bundle.js:89:27) at ZoneDelegate.webpackJsonp.../../../../../../../sandbox/master-project/node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9 876/_karma_webpack_/polyfills.bundle.js:2704:26) at AsyncTestZoneSpec.webpackJsonp.../../../../../../../sandbox/master-project/node_modules/zone.js/dist/async-test.js.AsyncTestZoneSpec.onInvoke ( http://localhost:9876/_karma_webpack_/vendor.bundle.js:55972:39) at ProxyZoneSpec.webpackJsonp.../../../../../../../sandbox/master-project/node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (http://localh Chrome 59.0.3071 (Windows 10 0.0.0): Executed 3 of 3 (1 FAILED) (0.225 secs / 0.204 secs) ``` After: ``` kamik@T460p MINGW64 D:/sandbox/master-project (master) $ ng test --sr 10% building modules 1/1 modules 0 active13 07 2017 16:37:59.478:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0:9876/ 13 07 2017 16:37:59.481:INFO [launcher]: Launching browser Chrome with unlimited concurrency 13 07 2017 16:37:59.487:INFO [launcher]: Starting browser Chrome 13 07 2017 16:38:08.559:INFO [Chrome 59.0. 3071 (Windows 10 0.0.0)]: Connected on socket ixHN4NzVFTEW1iYiAAAA with id 92819713 Chrome 59.0.3071 (Windows 10 0.0.0) AppComponent should have as title 'app' FAILED Expected 'app' to equal 'forced bug'. at Object.<anonymous> D:/sandbox/master-project/src/app/app.component.spec.ts:23:23) at ZoneDelegate.webpackJsonp.../../../../../../../sandbox/master-project/node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke D:/sandbox/master-p roject/node_modules/zone.js/dist/zone.js:391:1) at AsyncTestZoneSpec.webpackJsonp.../../../../../../../sandbox/master-project/node_modules/zone.js/dist/async-test.js.AsyncTestZoneSpec.onInvoke D :/sandbox/master-project/node_modules/zone.js/dist/async-test.js:49:1) at ProxyZoneSpec.webpackJsonp.../../../../../../../sandbox/master-project/node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke D:/sandbox/mas ter-project/node_modules/zone.js/dist/proxy.js:76:1) Chrome 59.0.3071 (Windows 10 0.0.0): Executed 2 of 3 (1 FAILED) (0 secs / 0.367 secs) Chrome 59.0.3071 (Windows 10 0.0.0) AppComponent should have as title 'app' FAILED Expected 'app' to equal 'forced bug'. at Object.<anonymous> D:/sandbox/master-project/src/app/app.component.spec.ts:23:23) at ZoneDelegate.webpackJsonp.../../../../../../../sandbox/master-project/node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke D:/sandbox/master-p roject/node_modules/zone.js/dist/zone.js:391:1) at AsyncTestZoneSpec.webpackJsonp.../../../../../../../sandbox/master-project/node_modules/zone.js/dist/async-test.js.AsyncTestZoneSpec.onInvoke D :/sandbox/master-project/node_modules/zone.js/dist/async-test.js:49:1) at ProxyZoneSpec.webpackJsonp.../../../../../../../sandbox/master-project/node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke D:/sandbox/mas Chrome 59.0.3071 (Windows 10 0.0.0): Executed 3 of 3 (1 FAILED) (0.421 secs / 0.4 secs) ``` Fix #6583
1 parent e66c26c commit 455d56e

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"isbinaryfile": "^3.0.0",
6767
"istanbul-instrumenter-loader": "^2.0.0",
6868
"json-loader": "^0.5.4",
69+
"karma-source-map-support": "^1.2.0",
6970
"less": "^2.7.2",
7071
"less-loader": "^4.0.5",
7172
"license-webpack-plugin": "^0.4.3",

packages/@angular/cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"inquirer": "^3.0.0",
5454
"isbinaryfile": "^3.0.0",
5555
"json-loader": "^0.5.4",
56+
"karma-source-map-support": "^1.2.0",
5657
"less": "^2.7.2",
5758
"less-loader": "^4.0.5",
5859
"license-webpack-plugin": "^0.4.2",

packages/@angular/cli/plugins/karma.ts

+41-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import { Pattern } from './glob-copy-webpack-plugin';
88
import { WebpackTestConfig, WebpackTestOptions } from '../models/webpack-test-config';
99
import { KarmaWebpackThrowError } from './karma-webpack-throw-error';
1010

11+
/**
12+
* Enumerate needed (but not require/imported) dependencies from this file
13+
* to let the dependency validator know they are used.
14+
*
15+
* require('karma-source-map-support')
16+
*/
17+
18+
1119
const getAppFromConfig = require('../utilities/app-utils').getAppFromConfig;
1220

1321
let blocked: any[] = [];
@@ -54,6 +62,22 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
5462
progress: true,
5563
}, config.angularCli);
5664

65+
if (testConfig.sourcemaps) {
66+
// Add a reporter that fixes sourcemap urls.
67+
config.reporters.unshift('@angular/cli');
68+
69+
// Code taken from https://github.com/tschaub/karma-source-map-support.
70+
// We can't use it directly because we need to add it conditionally in this file, and karma
71+
// frameworks cannot be added dynamically.
72+
const smsPath = path.dirname(require.resolve('source-map-support'));
73+
const ksmsPath = path.dirname(require.resolve('karma-source-map-support'));
74+
75+
addKarmaFiles(config.files, [
76+
{ pattern: path.join(smsPath, 'browser-source-map-support.js'), watched: false },
77+
{ pattern: path.join(ksmsPath, 'client.js'), watched: false }
78+
], true);
79+
}
80+
5781
// Add assets. This logic is mimics the one present in GlobCopyWebpackPlugin.
5882
if (appConfig.assets) {
5983
config.proxies = config.proxies || {};
@@ -227,9 +251,25 @@ function requestBlocker() {
227251
};
228252
}
229253

230-
// Also export karma-webpack and karma-sourcemap-loader.
254+
// Strip the server address and webpack scheme (webpack://) from error log.
255+
const initSourcemapReporter: any = function (baseReporterDecorator: any) {
256+
baseReporterDecorator(this);
257+
const urlRegexp = /\(http:\/\/localhost:\d+\/_karma_webpack_\/webpack:\//gi;
258+
259+
this.onSpecComplete = function (_browser: any, result: any) {
260+
if (!result.success && result.log.length > 0) {
261+
result.log.forEach((log: string, idx: number) => {
262+
result.log[idx] = log.replace(urlRegexp, '');
263+
});
264+
}
265+
};
266+
};
267+
268+
initSourcemapReporter.$inject = ['baseReporterDecorator'];
269+
231270
module.exports = Object.assign({
232271
'framework:@angular/cli': ['factory', init],
233272
'preprocessor:@angular/cli': ['factory', preprocessor],
273+
'reporter:@angular/cli': ['type', initSourcemapReporter],
234274
'middleware:angularCliBlocker': ['factory', requestBlocker]
235275
});

yarn.lock

+7-1
Original file line numberDiff line numberDiff line change
@@ -2950,6 +2950,12 @@ jsprim@^1.2.2:
29502950
json-schema "0.2.3"
29512951
verror "1.3.6"
29522952

2953+
karma-source-map-support@^1.2.0:
2954+
version "1.2.0"
2955+
resolved "https://registry.yarnpkg.com/karma-source-map-support/-/karma-source-map-support-1.2.0.tgz#1bf81e7bb4b089627ab352ec4179e117c406a540"
2956+
dependencies:
2957+
source-map-support "^0.4.1"
2958+
29532959
kind-of@^2.0.1:
29542960
version "2.0.1"
29552961
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-2.0.1.tgz#018ec7a4ce7e3a86cb9141be519d24c8faa981b5"
@@ -4758,7 +4764,7 @@ source-map-loader@^0.2.0:
47584764
loader-utils "~0.2.2"
47594765
source-map "~0.1.33"
47604766

4761-
source-map-support@^0.4.0, source-map-support@^0.4.2:
4767+
source-map-support@^0.4.0, source-map-support@^0.4.1, source-map-support@^0.4.2:
47624768
version "0.4.15"
47634769
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.15.tgz#03202df65c06d2bd8c7ec2362a193056fef8d3b1"
47644770
dependencies:

0 commit comments

Comments
 (0)