Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): use istanbul-lib-instrument direc…
Browse files Browse the repository at this point in the history
…tly for karma code coverage

The `istanbul-lib-instrument` package provides the required functionality needed
to instrument code for test coverage within the context of the Angular CLI.
Since the build pipeline already contains a customized babel preset, this package
can be integrated directly into the pipeline.
This reduces the number of dependencies required for `@angular-devkit/build-angular`
including the deprecated `inflight` package.

(cherry picked from commit fb2981d)
  • Loading branch information
clydin authored and alan-agius4 committed Jun 18, 2024
1 parent bdd168f commit 86e031d
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@
"ansi-colors": "4.1.3",
"autoprefixer": "10.4.19",
"babel-loader": "9.1.3",
"babel-plugin-istanbul": "6.1.1",
"browser-sync": "3.0.2",
"browserslist": "^4.21.5",
"buffer": "6.0.3",
Expand All @@ -145,6 +144,7 @@
"husky": "9.0.11",
"ini": "4.1.2",
"inquirer": "9.2.22",
"istanbul-lib-instrument": "6.0.2",
"jasmine": "^5.0.0",
"jasmine-core": "~5.1.0",
"jasmine-spec-reporter": "~7.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/build_angular/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ ts_library(
"@npm//ansi-colors",
"@npm//autoprefixer",
"@npm//babel-loader",
"@npm//babel-plugin-istanbul",
"@npm//browserslist",
"@npm//copy-webpack-plugin",
"@npm//critters",
Expand All @@ -173,6 +172,7 @@ ts_library(
"@npm//http-proxy-middleware",
"@npm//https-proxy-agent",
"@npm//inquirer",
"@npm//istanbul-lib-instrument",
"@npm//jsonc-parser",
"@npm//karma",
"@npm//karma-source-map-support",
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/build_angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"ansi-colors": "4.1.3",
"autoprefixer": "10.4.19",
"babel-loader": "9.1.3",
"babel-plugin-istanbul": "6.1.1",
"browserslist": "^4.21.5",
"copy-webpack-plugin": "11.0.0",
"critters": "0.0.22",
Expand All @@ -36,6 +35,7 @@
"https-proxy-agent": "7.0.4",
"http-proxy-middleware": "3.0.0",
"inquirer": "9.2.22",
"istanbul-lib-instrument": "6.0.2",
"jsonc-parser": "3.2.1",
"karma-source-map-support": "1.4.0",
"less": "4.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import { NodePath, PluginObj, types } from '@babel/core';
import { Visitor, programVisitor } from 'istanbul-lib-instrument';
import assert from 'node:assert';

/**
* A babel plugin factory function for adding istanbul instrumentation.
*
* @returns A babel plugin object instance.
*/
export default function (): PluginObj {
const visitors = new WeakMap<NodePath, Visitor>();

return {
visitor: {
Program: {
enter(path, state) {
const visitor = programVisitor(types, state.filename, {
// Babel returns a Converter object from the `convert-source-map` package
inputSourceMap: (state.file.inputMap as undefined | { toObject(): object })?.toObject(),
});
visitors.set(path, visitor);

visitor.enter(path);
},
exit(path) {
const visitor = visitors.get(path);
assert(visitor, 'Instrumentation visitor should always be present for program path.');

visitor.exit(path);
visitors.delete(path);
},
},
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

declare module 'istanbul-lib-instrument' {
export interface Visitor {
enter(path: import('@babel/core').NodePath<types.Program>): void;
exit(path: import('@babel/core').NodePath<types.Program>): void;
}

export function programVisitor(
types: typeof import('@babel/core').types,
filePath?: string,
options?: { inputSourceMap?: object | null },
): Visitor;
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,7 @@ export default function (api: unknown, options: ApplicationPresetOptions) {
}

if (options.instrumentCode) {
plugins.push([
require('babel-plugin-istanbul').default,
{
inputSourceMap: options.instrumentCode.inputSourceMap ?? false,
cwd: options.instrumentCode.includedBasePath,
},
]);
plugins.push(require('../plugins/add-code-coverage').default);
}

if (needRuntimeTransform) {
Expand Down
Loading

0 comments on commit 86e031d

Please sign in to comment.