fix(@angular/build): load zone.js dynamically for library unit tests#33478
fix(@angular/build): load zone.js dynamically for library unit tests#33478clydin wants to merge 1 commit into
Conversation
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
There was a problem hiding this comment.
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.
| } else if (zoneTestingStrategy === 'dynamic-zone') { | ||
| zoneTestingSnippet = `try { | ||
| await import('zone.js'); | ||
| await import('zone.js/testing'); | ||
| } catch (e) { | ||
| console.error('DYNAMIC IMPORT ERROR:', e); | ||
| }`; | ||
| } |
There was a problem hiding this comment.
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\');';
}
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