Skip to content

Commit 086d74d

Browse files
committed
fix(@angular-devkit/build-angular): improve debugging experience
With this change we improve the debugging experience with 2 things. 1. Cleaner Karma stack traces in the terminal Before ``` Chrome 84.0.4147.105 (Mac OS 10.15.5) AppComponent should have as title 'foo' FAILED Error: Expected 'foo' to equal 'fox'. at <Jasmine> at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/projects/foo/src/app/app.component.spec.ts:22:23) at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:364:1) at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:292:1) Chrome 84.0.4147.105 (Mac OS 10.15.5): Executed 3 of 3 (1 FAILED) (0 secs / 0.214 secs) Chrome 84.0.4147.105 (Mac OS 10.15.5) AppComponent should have as title 'foo' FAILED Error: Expected 'foo' to equal 'fox'. at <Jasmine> at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/projects/foo/src/app/app.component.spec.ts:22:23) at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:364:1) at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zChrome 84.0.4147.105 (Mac OS 10.15.5): Executed 3 of 3 (1 FAILED) (0.225 secs / 0.214 secs) ``` After ``` Chrome 84.0.4147.105 (Mac OS 10.15.5) AppComponent should have as title 'foo' FAILED Error: Expected 'foo' to equal 'fox'. at <Jasmine> at UserContext.<anonymous> (projects/foo/src/app/app.component.spec.ts:22:23) at ZoneDelegate.invoke (node_modules/zone.js/dist/zone-evergreen.js:364:1) at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/zone-testing.js:292:1) Chrome 84.0.4147.105 (Mac OS 10.15.5): Executed 2 of 3 (1 FAILED) (0 secs / 0.225 secs) Chrome 84.0.4147.105 (Mac OS 10.15.5) AppComponent should have as title 'foo' FAILED Error: Expected 'foo' to equal 'fox'. at <Jasmine> at UserContext.<anonymous> (projects/foo/src/app/app.component.spec.ts:22:23) at ZoneDelegate.invoke (node_modules/zone.js/dist/zone-evergreen.js:364:1) Chrome 84.0.4147.105 (Mac OS 10.15.5): Executed 3 of 3 (1 FAILED) (0.281 secs / 0.237 secs) ``` 2. Reduce VS Code configuration Before ``` { "name": "ng test", "type": "chrome", "request": "launch", "url": "http://localhost:9876/debug.html", "webRoot": "${workspaceFolder}", "sourceMaps": true, "sourceMapPathOverrides": { "webpack:/*": "${webRoot}/*", "/./*": "${webRoot}/*", "/src/*": "${webRoot}/*", "/*": "*", "/./~/*": "${webRoot}/node_modules/*" } }, ``` After ``` { "name": "ng test", "type": "chrome", "request": "launch", "url": "http://localhost:9876/debug.html", "webRoot": "${workspaceFolder}", }, ```
1 parent ee7d8f6 commit 086d74d

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function getSourceMapDevTool(
9191
// inline sourcemaps as otherwise paths to sourcemaps will be broken in browser
9292
// `webpack:///` is needed for Visual Studio breakpoints to work properly as currently
9393
// there is no way to set the 'webRoot'
94-
sourceRoot: inlineSourceMap ? '' : 'webpack:///',
94+
sourceRoot: 'webpack:///',
9595
moduleFilenameTemplate: '[resource-path]',
9696
append: hiddenSourceMap ? false : undefined,
9797
exclude: vendorSourceMap ? undefined : /vendor.*\.js/,

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/karma.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,9 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
7070
successCb = config.buildWebpack.successCb;
7171
failureCb = config.buildWebpack.failureCb;
7272

73-
// When using code-coverage, auto-add coverage-istanbul.
74-
config.reporters = config.reporters || [];
75-
if (options.codeCoverage && config.reporters.indexOf('coverage-istanbul') === -1) {
76-
config.reporters.push('coverage-istanbul');
77-
}
78-
7973
// Add a reporter that fixes sourcemap urls.
8074
if (normalizeSourceMaps(options.sourceMap).scripts) {
81-
config.reporters.push('@angular-devkit/build-angular--sourcemap-reporter');
75+
config.reporters.unshift('@angular-devkit/build-angular--sourcemap-reporter');
8276

8377
// Code taken from https://github.com/tschaub/karma-source-map-support.
8478
// We can't use it directly because we need to add it conditionally in this file, and karma
@@ -92,8 +86,14 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
9286
], true);
9387
}
9488

95-
config.reporters.push('@angular-devkit/build-angular--event-reporter');
89+
config.reporters.unshift('@angular-devkit/build-angular--event-reporter');
9690

91+
// When using code-coverage, auto-add coverage-istanbul.
92+
config.reporters = config.reporters || [];
93+
if (options.codeCoverage && config.reporters.indexOf('coverage-istanbul') === -1) {
94+
config.reporters.unshift('coverage-istanbul');
95+
}
96+
9797
// Add webpack config.
9898
const webpackConfig = config.buildWebpack.webpackConfig;
9999
const webpackMiddlewareConfig = {
@@ -284,16 +284,13 @@ eventReporter.$inject = ['baseReporterDecorator', 'config'];
284284
// Strip the server address and webpack scheme (webpack://) from error log.
285285
const sourceMapReporter: any = function (this: any, baseReporterDecorator: any, config: any) {
286286
baseReporterDecorator(this);
287-
288287
muteDuplicateReporterLogging(this, config);
289288

290-
const urlRegexp = /http:\/\/localhost:\d+\/_karma_webpack_\/webpack:\//gi;
289+
const urlRegexp = /http:\/\/localhost:\d+\/_karma_webpack_\/(webpack:\/)?/gi;
291290

292291
this.onSpecComplete = function (_browser: any, result: any) {
293-
if (!result.success && result.log.length > 0) {
294-
result.log.forEach((log: string, idx: number) => {
295-
result.log[idx] = log.replace(urlRegexp, '');
296-
});
292+
if (!result.success) {
293+
result.log = result.log.map((l: string) => l.replace(urlRegexp, ''));
297294
}
298295
};
299296

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { writeMultipleFiles } from '../../utils/fs';
2+
import { ng } from '../../utils/process';
3+
import { expectToFail } from '../../utils/utils';
4+
5+
export default async function () {
6+
await writeMultipleFiles({
7+
'src/app/app.component.spec.ts': `
8+
describe('AppComponent', () => {
9+
it('failing test', () => {
10+
expect('1').toEqual('2');
11+
});
12+
});
13+
`,
14+
});
15+
16+
const { message } = await expectToFail(() => ng('test', '--no-watch'));
17+
if (message.includes('_karma_webpack_')) {
18+
throw new Error(`Didn't expect logs to server address and webpack scheme.\n${message}`);
19+
}
20+
21+
if (!message.includes('(src/app/app.component.spec.ts:4:25)')) {
22+
throw new Error(`Expected logs to contain relative path to (src/app/app.component.spec.ts:4:25)\n${message}`);
23+
}
24+
}

0 commit comments

Comments
 (0)