Skip to content

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

Open
clydin wants to merge 1 commit into
angular:mainfrom
clydin:fix/load-zone-for-library-tests
Open

fix(@angular/build): load zone.js dynamically for library unit tests#33478
clydin wants to merge 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

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 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.

Comment on lines +56 to 63
} else if (zoneTestingStrategy === 'dynamic-zone') {
zoneTestingSnippet = `try {
await import('zone.js');
await import('zone.js/testing');
} catch (e) {
console.error('DYNAMIC IMPORT ERROR:', e);
}`;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Swallowing the dynamic import error here and only logging it to console.error can make debugging difficult if zone.js or zone.js/testing fails to load at runtime. Since dynamic-zone is only selected when zone.js is resolved at build time, any runtime import failure is an exceptional state.

Letting the error propagate (similar to how the 'dynamic' strategy does) ensures the test runner fails fast with a clear stack trace pointing directly to the failed import, rather than continuing and failing later with cryptic errors (e.g., Zone is undefined).

  } else if (zoneTestingStrategy === 'dynamic-zone') {
    zoneTestingSnippet = 'await import(\'zone.js\');\n    await import(\'zone.js/testing\');';
  }

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

Labels

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

1 participant