Expose internal "instrumentForCoverage" option
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 58/100
- Tipo de issue
- Nueva funcionalidad
- Claridad
- Bien especificado
- Estado de actividad
- Tranquilo
- Stack tecnológico
- angular, typescript
- Área
- build-system, testing-qa
Línea de trabajo
Comienza con @angular/build/src/builders/application/schema.json y @angular/build/src/tools/esbuild/angular/compiler-plugin.js, y luego sigue cómo llegan las opciones del application builder al compiler plugin. Revisa las pruebas existentes del builder y del compiler-plugin antes de hacer disponible la opción, y considera terminado el trabajo cuando la opción sea aceptada, instrumente el código de aplicación previsto y excluya node_modules de la recopilación de cobertura.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Command
build
Description
E2E test runners like Cypress, in order to collect code coverage data, require the code served by the dev-server to be instrumented. I'd like the built-in builder to let me do exactly that, considering the fact that it can.
Describe the solution you'd like
I'd like to be able to specify "instrumentForCoverage": true in my @angular/build:application options and be done with it. I've patched my node modules to see what that would look like and it works like a charm: @cypress/code-coverage is generating the reports, the node_modules are excluded, and I'm able to use the same builder for E2E and prod builds.
In the next section I've collected some of the hoops I had to jump through to get close (but fail to attain) this level of integration. By the end of the read, I hope you'll agree with me that a built-in solution (considering it's already coded!) would be a step-up in terms of developer experience.
In the meantime, for reference, here's the quick-n-dirty patch.
diff --git a/node_modules/@angular/build/src/builders/application/schema.json b/node_modules/@angular/build/src/builders/application/schema.json
index 3ee8699..0b893a0 100755
--- a/node_modules/@angular/build/src/builders/application/schema.json
+++ b/node_modules/@angular/build/src/builders/application/schema.json
@@ -47,6 +47,10 @@
"type": "string",
"description": "Customize the base path for the URLs of resources in 'index.html' and component stylesheets. This option is only necessary for specific deployment scenarios, such as with Angular Elements or when utilizing different CDN locations."
},
+ "instrumentForCoverage": {
+ "type": "boolean",
+ "description": "Enables instrumentation to collect code coverage data for specific files."
+ },
"security": {
"description": "Security features to protect against XSS and other common attacks",
"type": "object",
diff --git a/node_modules/@angular/build/src/tools/esbuild/angular/compiler-plugin.js b/node_modules/@angular/build/src/tools/esbuild/angular/compiler-plugin.js
index 675ae15..ab42aab 100755
--- a/node_modules/@angular/build/src/tools/esbuild/angular/compiler-plugin.js
+++ b/node_modules/@angular/build/src/tools/esbuild/angular/compiler-plugin.js
@@ -354,7 +354,7 @@ function createCompilerPlugin(pluginOptions, compilationOrFactory, stylesheetBun
// A string indicates untransformed output from the TS/NG compiler.
// This step is unneeded when using esbuild transpilation.
const sideEffects = await hasSideEffects(request);
- const instrumentForCoverage = pluginOptions.instrumentForCoverage?.(request);
+ const instrumentForCoverage = pluginOptions.instrumentForCoverage; //?.(request);
contents = await javascriptTransformer.transformData(request, contents, true /* skipLinker */, sideEffects, instrumentForCoverage);
// Store as the returned Uint8Array to allow caching the fully transformed code
typeScriptFileCache.set(request, contents);
Describe alternatives you've considered
In my search, for Angular 20, I've found only one semi-working[1] solution that requires a 3rd-party builder (@angular-builder/custom-webpack) based on Webpack. That might sound like I haven't done any due diligence, when in fact I have.
-
@angular-builder/custom-esbuildwithesbuild-plugin-istanbuldoesn't work because theonLoadcallback of an esbuild plugin (such as the one defined by the istanbul plugin) is called only as long as no previous plugin handled the resource that's being loaded, and theangular-compilerhandles them all.- A possible solution could be a custom plugin that overrides the default namespace of each resource we may want to instrument (with
onResolve), which would effectively sidestep the Angular compiler BUT force the plugin author to reimplement the compilation. Very high effort.
- A possible solution could be a custom plugin that overrides the default namespace of each resource we may want to instrument (with
-
A Vite plugin (rather than an esbuild one) could work, but the Angular builder is not based on Vite.
@analogjs/vite-plugin-angularis, but it doesn't have a builder, only an executor for@nx, which is another can of worms. In short, the migration path isn't straightforward at all, and let's remember: this is all to add instrumentation! -
I've considered trying Angular Rspack, but it's still experimental. I want my E2E tests to be as close to my production environment as they can get, and using two different builders (one for prod and one for e2e) is not an option.
-
Finally, of course, the
@angular/buildpackage doesn't do instrumentation. Not through the builder schema anyway.
Apart from my lack of Webpack knowledge (see footnote), it appears to me that the setup I found is still not ideal. Some reasons:
- Angular moved away from Webpack. The happy path--aka, the officially supported path--is esbuild, not Webpack.
- Webpack is slower than esbuild in most cases. Not a big deal but I'm sure there are some peeps who'd be very impacted by that.
- No third-party esbuild-based builder that makes use of the official devkit/extends the
@angular/buildbuilder will ever be able to offer a different experience. Any plugin passed to the builder will always be appended to the end of the list of plugins, after the angular compiler. Ergo, noonLoadcallbacks.
[1] I say semi-working 'cause I don't know Webpack enough--but who does 😆--and didn't manage to add the instrumentation to the un-chunked code, which regrettably leads to instrumented node_modules
- Lenguaje dominante
- TypeScript
- Estrellas
- 27k
- Forks
- 11.8k
- Merge medio
- 17 h 25 min
- PR fusionados (30 d)
- 183
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de angular/angular-cli
-
Can't use an array of hostnames in --allowedHosts cli parameter in @angular/build:dev-server Abiertoarea: @angular/build gemini-triaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
angular/angular-cli#33955 ·
-
area: @angular/cli gemini-triaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
angular/angular-cli#33055 · 1 comentario · 3 reacciones ·
-
angular/build:library area: @angular/build gemini-triaged
angular/angular-cli#34131 · 1 asignado ·
-
angular/build:library area: @angular/build gemini-triaged
angular/angular-cli#34130 · 1 asignado ·
-
angular/build:library area: @angular/build gemini-triaged
angular/angular-cli#34128 · 1 asignado ·
Todos los issues de angular/angular-cli
Issues similares
-
[Bug]: Discord Activity titles with emoji are rejected as over 80 characters when they are not Abiertoclawsweeper:linked-pr-open clawsweeper:no-new-fix-pr clawsweeper:source-repro impact:message-loss issue-rating: 🦞 diamond lobster maturity:stable P2
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
Eynzof/Hermes-CN-Desktop#616 ·
-
ZCode 3.14.3 に対応する Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
supermomonga/zcode-acp#24 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
growthbook/growthbook#7100 ·
-
triage
Dificultad 1/5 1-3 horas Aptitud para principiantes 88/100