fix(frontend): guarantee JIT compiler in vitest runs to stop PlatformLocation flake#636
Merged
Merged
Conversation
…Location flake The unit-test builder keeps Angular packages external, so vitest evaluates raw fesm2022 chunks whose partial declarations (ɵɵngDeclareInjectable/ ɵɵngDeclareFactory) compile eagerly and require @angular/compiler. Its presence was incidental — loaded transitively via @angular/core/testing in the builder's init-testbed setup — so specs with no static Angular imports (app.spec.ts dynamic-imports './app') could evaluate an unlinked @angular/common chunk first and fail with "The injectable 'PlatformLocation' needs to be compiled using the JIT compiler, but '@angular/compiler' is not available" (angular/angular-cli#31993). - add src/test-setup.ts importing @angular/compiler, wired via the test target's setupFiles and included in tsconfig.spec.json - add src/test-setup.spec.ts guarding the invariant deterministically - bump the first shared-view.page spec to 15s: it pays the one-time dynamic page-chunk import, which can exceed 5s under full-suite load Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
On a clean checkout,
npm testinfrontend/ai.clientcan fail insrc/app/app.spec.tswith:raised from
import('./app')→@angular/router→@angular/common_platform_location-chunk. Both tests in the file fail (module-level import failure). The failure is flaky and environment/timing dependent.Root cause
@angular/build:unit-testbuilds specs withexternalPackages: true, so@angular/*packages are never bundled/linked — vitest loads the raw npm fesm2022 files at runtime (_platform_location-chunk.mjsis a file shipped inside@angular/common21.2).ɵɵngDeclareInjectable/ɵɵngDeclareFactory) that compile eagerly at module evaluation viagetCompilerFacade— they require@angular/compilerthe moment the module is evaluated, not at first injection.init-testbedsetup imports@angular/core/testing, which transitively evaluates@angular/compiler. Nothing guarantees that ordering.app.spec.tsis uniquely exposed because it has no static Angular imports — when the evaluation order breaks, the whole file dies at import time.Upstream reports of the same failure mode: angular/angular-cli#31993, angular/angular-cli#32095; the upstream remedy (angular/angular-cli#32304) is the same one applied here — guarantee the compiler is loaded.
Fix
src/test-setup.ts—import '@angular/compiler';, wired via the test target'ssetupFilesinangular.json, so the JIT fallback is an explicit invariant in every vitest worker instead of an accident of import order.tsconfig.spec.json— includes the setup file in the spec TS program (the builder hard-errors otherwise).src/test-setup.spec.ts— guard spec mirroring app.spec.ts's shape (no static Angular imports): asserts the compiler facade is registered and that an unlinked partial-declared injectable actually JIT-compiles. If the setup file is ever removed, this fails deterministically instead of the suite failing flakily.shared-view.page.spec.ts— separate flake caught while soak-testing (1-in-9 full-suite runs): the file's first test pays the one-time dynamic page-chunk import and exceeded vitest's 5s default under parallel load (5011ms observed). Bumped that single test to 15s.Verification
ng testruns green (126 files / 1449 tests) on the pinned dependency tree (npm ci), including cold-cache runs (.angular/cache+node_modules/.viteremoved).setup-test-setup.jsappears as a build entry point and runs before specs.🤖 Generated with Claude Code