GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(@angular/build): preserve component initialization in Karma test bundles by nagahara-naoki · Pull Request #34668 · angular/angular-cli

Conversation

@nagahara-naoki

@nagahara-naoki nagahara-naoki commented Nov 7, 2026

Copy link
Copy Markdown

PR Checklist

  • The commit message follows our guidelines: https://github.com/angular/angular-cli/blob/main/CONTRIBUTING.md#-commit-message-guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (not applicable: internal Karma bundling fix; no new options or public API)

PR Type

  • Bugfix

What is the current behavior?

Fixes #34250.

With @angular/build:karma, a component imported through a barrel can be captured as undefined in another component's imports array. This depends on the combination of spec entry points: an async spec importing the component directly can cause lazy module initialization to cross shared-chunk boundaries. The consuming decorator can run before the component is initialized.

The regression fixture includes:

  1. A standalone layout component re-exported by a barrel.
  2. A dialog importing that barrel and used by two specs.
  3. A page importing the barrel and used by another spec.
  4. An async spec importing the layout directly.

Against main (2c6e8b970), the browser reports 2 failed, 2 successful. Both dialog specs throw Cannot read properties of undefined (reading 'ɵcmp').

What is the new behavior?

The Karma build routes the discovered spec entry points through a shared virtual module that imports all selected specs. This keeps spec initialization in one module graph, avoiding the problematic partitioning across independent spec entry points.

  • Preserve the existing spec-* entry names, including basename-collision handling.
  • Keep code splitting enabled so Angular and its testing environment remain shared with the setup entry point.
  • Include the original spec files as TypeScript root files, since the build entry points now refer to the virtual module.
  • Preserve include/exclude selection: the virtual module only imports the specs collected by the existing discovery logic.

The same four browser specs now all pass. The added integration test exercises real Karma/Chrome execution rather than only checking generated bundle text.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Simply setting disableCodeSplitting: true, as suggested by the earlier Vitest fix in #33729, was insufficient for Karma in this reproduction: it produced testing-environment initialization errors. This change retains shared framework chunks instead.

Validation on macOS arm64, Node.js 24.22.2, pnpm 12.16.1, and Chrome Headless 159:

  • pnpm bazel test //packages/angular/build:karma_integration_tests --config=no-sharding --test_output=errors: 42 integration specs, 0 failures.
  • ESLint for the changed implementation passes. The repository ESLint configuration excludes builder integration fixtures.
  • Prettier checks for both changed files and git diff --check pass.

The full repository and E2E suites were not run. Large-suite build size/performance and dynamic addition of spec files during watch mode were not separately benchmarked or validated.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gemini-code-assist[bot] reacted with eyes emoji
All reactions
  • 👀 1 reaction
@nagahara-naoki
fix(@angular/build): preserve component initialization in Karma test …
d277ee9
…bundles

Load selected Karma specs through a shared virtual module while retaining spec entry names and shared framework chunks. Add a browser regression for barrel imports combined with async specs.

Fixes angular#34250
@angular-robot angular-robot Bot added the area: @angular/build label Nov 7, 2026
gemini-code-assist[bot]
gemini-code-assist Bot reviewed Nov 7, 2026
View reviewed changes

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the Karma application builder to group spec files into a single module graph using a virtual module plugin. This prevents cross-chunk lazy initialization from exposing uninitialized barrel exports. Additionally, a behavior test is added to verify barrel imports with async tests. The review feedback suggests appending the collected spec files to buildOptions.rootFiles instead of overwriting it, which prevents potential loss of existing root files.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/angular/build/src/builders/karma/application_builder.ts Outdated
entryPoints.set(name, 'angular:karma-specs');
}
}
buildOptions.rootFiles = specFiles;

@gemini-code-assist gemini-code-assist Bot Nov 7, 2026

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent potentially overwriting any existing rootFiles that might be set on buildOptions in the future or by other setup steps, it is safer to append the new specFiles to the existing array if it exists, rather than directly overwriting it.

  buildOptions.rootFiles = buildOptions.rootFiles
    ? [...buildOptions.rootFiles, ...specFiles]
    : specFiles;

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nagahara-naoki nagahara-naoki mentioned this pull request Nov 7, 2026
karma builder: component imported through a barrel is undefined in TestBed when another spec has an async test (cross-chunk lazy init, karma counterpart of #33728) #34250
Open
@nagahara-naoki
fixup! fix(@angular/build): preserve component initialization in Karm…
bbcf1b7
…a test bundles
@alan-agius4

alan-agius4 commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Thank you for taking the time to investigate #34250 and put together this PR.

However, we are going to close this PR as this approach introduces several regressions and architectural issues in @angular/build:karma:

  1. Breaks custom main (src/test.ts) and TypeScript polyfills: Setting buildOptions.rootFiles to only specFiles overrides the rootNames loaded from tsconfig.spec.json. Any options.main file (test_main) or .ts polyfill is excluded from the TypeScript compilation, causing builds where main imports @angular/core/testing to fail with File '...' not found in TypeScript compilation.
  2. Redundant bundle emission and script loading: Mapping all $N$ spec-* entry points to the same 'angular:karma-specs' virtual module causes esbuild to emit $N$ identical wrapper bundles (spec-*.js and .js.map) and Karma to load $N$ <script type="module"> tags in the browser, where the first script executes all specs in the suite and the remaining $N - 1$ scripts are no-ops.
  3. Loss of per-spec isolation and incomplete fix: Combining all specs into a single virtual entry point removes per-spec module evaluation isolation (a top-level error in one spec prevents subsequent specs from registering), and the same esbuild chunk-splitting initialization order issue can still occur between test_main and angular:karma-specs.

Given that Karma is deprecated in favor of @angular/build:unit-test, we do not plan to restructure the Karma builder's entry-point bundling around a single virtual module.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@alan-agius4 alan-agius4 closed this Nov 9, 2026
@nagahara-naoki

nagahara-naoki commented Nov 9, 2026

Copy link
Copy Markdown
Author

Thanks for taking the time to review this and explain the trade-offs! I appreciate you pointing out the impact on custom test setups and per-spec isolation. I understand the decision to close the PR given Karma’s deprecation.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@gemini-code-assist gemini-code-assist[bot] gemini-code-assist[bot] left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

area: @angular/build

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

karma builder: component imported through a barrel is undefined in TestBed when another spec has an async test (cross-chunk lazy init, karma counterpart of #33728)

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@nagahara-naoki @alan-agius4
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.