Skip to content

Commit a7f2346

Browse files
alan-agius4mgechev
authored andcommitted
fix(@angular-devkit/build-angular): normalize sourceMap options in karma webpack plugin
`sourceMap` option can be either a boolean or an object,we need to normalize it before trying to get the `script` value. Fixes #14457
1 parent ffaf764 commit a7f2346

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { getWebpackStatsConfig } from '../models/webpack-configs/stats';
1919
import { createConsoleLogger } from '@angular-devkit/core/node';
2020
import { logging } from '@angular-devkit/core';
2121
import { WebpackTestOptions } from '../models/build-options';
22+
import { normalizeSourceMaps } from '../../utils/index';
2223

2324
/**
2425
* Enumerate needed (but not require/imported) dependencies from this file
@@ -78,7 +79,7 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
7879
}
7980

8081
// Add a reporter that fixes sourcemap urls.
81-
if (options.sourceMap.scripts) {
82+
if (normalizeSourceMaps(options.sourceMap).scripts) {
8283
config.reporters.unshift('@angular-devkit/build-angular--sourcemap-reporter');
8384

8485
// Code taken from https://github.com/tschaub/karma-source-map-support.
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,58 @@
11
import { writeFile } from '../../utils/fs';
2-
import { execAndWaitForOutputToMatch, killAllProcesses } from '../../utils/process';
2+
import { ng } from '../../utils/process';
33
import { updateJsonFile } from '../../utils/project';
44

55
export default async function () {
6+
await writeFile('src/app/app.component.spec.ts', `
7+
it('show fail', () => {
8+
expect(undefined).toBeTruthy();
9+
});
10+
`);
11+
612
await updateJsonFile('angular.json', configJson => {
713
const appArchitect = configJson.projects['test-project'].architect;
814
appArchitect.test.options.sourceMap = {
915
scripts: true,
1016
};
1117
});
1218

13-
await writeFile('src/app/app.component.spec.ts', `
14-
it('show fail', () => {
15-
expect(undefined).toBeTruthy();
16-
});
17-
`);
19+
// when sourcemaps are 'on' the stacktrace will point to the spec.ts file.
20+
try {
21+
await ng('test', '--watch', 'false');
22+
throw new Error('ng test should have failed.');
23+
} catch (error) {
24+
if (!error.message.includes('app.component.spec.ts')) {
25+
throw error;
26+
};
27+
}
1828

19-
// when sourcemaps are not working the stacktrace won't point to the spec.ts file.
29+
await updateJsonFile('angular.json', configJson => {
30+
const appArchitect = configJson.projects['test-project'].architect;
31+
appArchitect.test.options.sourceMap = true;
32+
});
33+
34+
// when sourcemaps are 'on' the stacktrace will point to the spec.ts file.
2035
try {
21-
await execAndWaitForOutputToMatch(
22-
'ng',
23-
['test', '--watch', 'false'],
24-
/app\.component\.spec\.ts/,
25-
);
26-
} finally {
27-
killAllProcesses();
36+
await ng('test', '--watch', 'false');
37+
throw new Error('ng test should have failed.');
38+
} catch (error) {
39+
if (!error.message.includes('app.component.spec.ts')) {
40+
throw error;
41+
};
42+
}
43+
44+
await updateJsonFile('angular.json', configJson => {
45+
const appArchitect = configJson.projects['test-project'].architect;
46+
appArchitect.test.options.sourceMap = false;
47+
});
48+
49+
// when sourcemaps are 'off' the stacktrace won't point to the spec.ts file.
50+
try {
51+
await ng('test', '--watch', 'false');
52+
throw new Error('ng test should have failed.');
53+
} catch (error) {
54+
if (!error.message.includes('main.js')) {
55+
throw error;
56+
};
2857
}
2958
}

0 commit comments

Comments
 (0)