Skip to content

Commit ef8815d

Browse files
alan-agius4clydin
authored andcommittedNov 9, 2021
fix(@angular-devkit/build-angular): improve sourcemap fidelity during code-coverage
We now pass the input sourcemap to istanbul babel plugin, since this is used directly by the instrumenter https://github.com/istanbuljs/babel-plugin-istanbul/blob/d58c92a7de5e8ac84c598e02325b05f5b9800107/src/index.js#L126-L129 and is needed to remapped instrumented code back to the original source. Previously, the lack of this caused incorrect reports of uncovered and coverage code and also incorrectly mappings in the HTML report. Closes #22118 (cherry picked from commit 5990ed9)
1 parent ff4538e commit ef8815d

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed
 

‎packages/angular_devkit/build_angular/src/babel/presets/application.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface ApplicationPresetOptions {
4949
forceAsyncTransformation?: boolean;
5050
instrumentCode?: {
5151
includedBasePath: string;
52+
inputSourceMap: unknown;
5253
};
5354
optimize?: {
5455
looseEnums: boolean;
@@ -249,7 +250,10 @@ export default function (api: unknown, options: ApplicationPresetOptions) {
249250
if (options.instrumentCode) {
250251
plugins.push([
251252
require('babel-plugin-istanbul').default,
252-
{ inputSourceMap: false, cwd: options.instrumentCode.includedBasePath },
253+
{
254+
inputSourceMap: options.instrumentCode.inputSourceMap ?? false,
255+
cwd: options.instrumentCode.includedBasePath,
256+
},
253257
]);
254258
}
255259

‎packages/angular_devkit/build_angular/src/babel/webpack-loader.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default custom<ApplicationPresetOptions>(() => {
7373
});
7474

7575
return {
76-
async customOptions(options, { source }) {
76+
async customOptions(options, { source, map }) {
7777
const { i18n, scriptTarget, aot, optimize, instrumentCode, ...rawOptions } =
7878
options as AngularBabelLoaderOptions;
7979

@@ -176,6 +176,7 @@ export default custom<ApplicationPresetOptions>(() => {
176176
// `babel-plugin-istanbul` has it's own includes but we do the below so that we avoid running the the loader.
177177
customOptions.instrumentCode = {
178178
includedBasePath: instrumentCode.includedBasePath,
179+
inputSourceMap: map,
179180
};
180181

181182
shouldProcess = true;

‎packages/angular_devkit/build_angular/src/builders/karma/tests/behavior/code-coverage_spec.ts

+32
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import { tags } from '@angular-devkit/core';
910
import { last, tap } from 'rxjs/operators';
1011
import { promisify } from 'util';
1112
import { execute } from '../../index';
@@ -85,5 +86,36 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
8586
)
8687
.toPromise();
8788
});
89+
90+
it('should remapped instrumented code back to the original source', async () => {
91+
await harness.modifyFile('src/app/app.component.ts', (content) => {
92+
return content.replace(
93+
`title = 'app'`,
94+
tags.stripIndents`
95+
title = 'app';
96+
97+
async foo() {
98+
return 'foo';
99+
}
100+
`,
101+
);
102+
});
103+
104+
harness.useTarget('test', {
105+
...BASE_OPTIONS,
106+
codeCoverage: true,
107+
});
108+
109+
const { result } = await harness.executeOnce();
110+
expect(result?.success).toBeTrue();
111+
112+
await setTimeoutPromise(1000);
113+
114+
harness
115+
.expectFile('coverage/app.component.ts.html')
116+
.content.toContain(
117+
'<span class="fstat-no" title="function not covered" >async </span>foo()',
118+
);
119+
});
88120
});
89121
});

0 commit comments

Comments
 (0)
Please sign in to comment.