Skip to content

fix(@angular/cli): fix ng test report sourcemaps #6968

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

Merged
merged 1 commit into from
Jul 19, 2017
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"isbinaryfile": "^3.0.0",
"istanbul-instrumenter-loader": "^2.0.0",
"json-loader": "^0.5.4",
"karma-source-map-support": "^1.2.0",
"less": "^2.7.2",
"less-loader": "^4.0.5",
"license-webpack-plugin": "^0.4.3",
Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"inquirer": "^3.0.0",
"isbinaryfile": "^3.0.0",
"json-loader": "^0.5.4",
"karma-source-map-support": "^1.2.0",
"less": "^2.7.2",
"less-loader": "^4.0.5",
"license-webpack-plugin": "^0.4.2",
Expand Down
42 changes: 41 additions & 1 deletion packages/@angular/cli/plugins/karma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ 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')
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment block necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, otherwise the dependency validator thinks this dependency is not used. It's needed but it's required via require.resolve() instead of require(), so the automatic detection does not work. The same workaround is used in webpack-configs/common.ts for loaders.



const getAppFromConfig = require('../utilities/app-utils').getAppFromConfig;

let blocked: any[] = [];
Expand Down Expand Up @@ -54,6 +62,22 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
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 || {};
Expand Down Expand Up @@ -227,9 +251,25 @@ function requestBlocker() {
};
}

// Also export karma-webpack and karma-sourcemap-loader.
// 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]
});
8 changes: 7 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2950,6 +2950,12 @@ jsprim@^1.2.2:
json-schema "0.2.3"
verror "1.3.6"

karma-source-map-support@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/karma-source-map-support/-/karma-source-map-support-1.2.0.tgz#1bf81e7bb4b089627ab352ec4179e117c406a540"
dependencies:
source-map-support "^0.4.1"

kind-of@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-2.0.1.tgz#018ec7a4ce7e3a86cb9141be519d24c8faa981b5"
Expand Down Expand Up @@ -4758,7 +4764,7 @@ source-map-loader@^0.2.0:
loader-utils "~0.2.2"
source-map "~0.1.33"

source-map-support@^0.4.0, source-map-support@^0.4.2:
source-map-support@^0.4.0, source-map-support@^0.4.1, source-map-support@^0.4.2:
version "0.4.15"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.15.tgz#03202df65c06d2bd8c7ec2362a193056fef8d3b1"
dependencies:
Expand Down