Skip to content

Support instrumentForCoverage for E2E coverage (Cypress) without patching @angular/build #59

@Aukevanoost

Description

@Aukevanoost

Problem

Angular's esbuild application builder has an internal instrumentForCoverage filter that drives Istanbul instrumentation, but it's not in the public schema (additionalProperties: false) — only the Karma builder uses it for unit tests. So there's no supported way to instrument the served bundles for E2E, and @cypress/code-coverage can't produce LCOV for Sonar.

The usual workaround patches node_modules/@angular/build directly, which is wiped on every install and pinned to one Angular version.

Proposal

Expose it through the native-federation builder, which already owns both call sites into Angular's builder. Add instrumentForCoverage: boolean (+ optional codeCoverageExclude: string[]) to the adapter schema and inject the filter into:

  • the dev-server / E2E path — inside createInternalAngularBuilder, before buildApplicationInternal (Cypress's path; must go here because serveWithVite re-fetches options as JSON, which can't carry a function);
  • the plain build path — spread onto buildApplication's options.

Opt-in via angular.json, no node_modules patch:

"cypress-coverage": {
  "target": "my-app:build:cypress-coverage",
  "instrumentForCoverage": true,
  "codeCoverageExclude": ["**/*.stories.ts"]   // optional
}

Notes

  • Filter logic is copied from @angular/build's karma/coverage.ts (not imported — those helpers aren't in @angular/build/private and sit on an unstable path). projectSourceRoot comes from context.getProjectMetadata(); excludes use built-in fs.globSync (no new dependency).
  • Verified against @angular/build@22.0.0; internals match the 21.2.x line the original patch targeted.
  • Risk: instrumentForCoverage is a non-SemVer internal option — if Angular drops it, injection becomes a no-op. Worth re-checking on Angular major bumps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions