Skip to content

fix(@angular/build): load zone.js dynamically for library unit tests#33478

Merged
alan-agius4 merged 1 commit into
angular:mainfrom
clydin:fix/load-zone-for-library-tests
Jun 29, 2026
Merged

fix(@angular/build): load zone.js dynamically for library unit tests#33478
alan-agius4 merged 1 commit into
angular:mainfrom
clydin:fix/load-zone-for-library-tests

Conversation

@clydin

@clydin clydin commented Jun 27, 2026

Copy link
Copy Markdown
Member

When unit testing library targets with the unit-test builder, the build target (ng-packagr) does not have a 'polyfills' configuration. This caused the builder to fall back to the 'dynamic' zone testing strategy, which only dynamically imports zone.js/testing at runtime if Zone is already defined. Since the main zone.js library was never loaded, Zone was undefined, and library tests relying on TestBed/fakeAsync failed.

This commit introduces a new 'dynamic-zone' zone testing strategy. If polyfills is undefined (meaning we are running a library target) and zone.js is installed/resolvable, both zone.js and zone.js/testing are dynamically imported at startup, resolving the failure.

Fixes #33477

@clydin clydin added the target: patch This PR is targeted for the next patch release label Jun 27, 2026
@clydin clydin marked this pull request as ready for review June 27, 2026 01:40

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

Copy link
Copy Markdown

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 introduces a new 'dynamic-zone' testing strategy to dynamically load 'zone.js' and 'zone.js/testing' when testing a library where polyfills are undefined. It also adds a corresponding integration test. The feedback suggests letting the dynamic import errors propagate instead of catching and logging them, ensuring that the test runner fails fast with a clear stack trace if the imports fail.

When unit testing library targets with the unit-test builder, the build
target (ng-packagr) does not have a 'polyfills' configuration. This
caused the builder to fall back to the 'dynamic' zone testing strategy,
which only dynamically imports zone.js/testing at runtime if Zone is
already defined. Since the main zone.js library was never loaded, Zone
was undefined, and library tests relying on TestBed/fakeAsync failed.

This commit introduces a new 'dynamic-zone' zone testing strategy. If
polyfills is undefined (meaning we are running a library target) and
zone.js is installed/resolvable, both zone.js and zone.js/testing are
dynamically imported at startup, resolving the failure.

Fixes angular#33477
@clydin clydin force-pushed the fix/load-zone-for-library-tests branch from f28bcfb to 06359f1 Compare June 29, 2026 12:35
@clydin clydin added the action: review The PR is still awaiting reviews from at least one requested reviewer label Jun 29, 2026
@clydin clydin requested a review from alan-agius4 June 29, 2026 12:36
@clydin clydin added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jun 29, 2026
@alan-agius4 alan-agius4 merged commit 61e25fb into angular:main Jun 29, 2026
41 checks passed
@alan-agius4

Copy link
Copy Markdown
Collaborator

This PR was merged into the repository. The changes were merged into the following branches:

@clydin clydin deleted the fix/load-zone-for-library-tests branch June 29, 2026 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: @angular/build target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zone.js is not automatically loaded when running unit-test builder for Angular libraries

2 participants