Repository navigation
fix(@angular/build): flush pending JIT NgModule scopes in Vitest beforeAll - #34815
Merged
Merged
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the Vitest unit test runner to preserve JIT NgModule scopes across spec files when isolation is disabled. It achieves this by importing and invoking ɵflushModuleScopingQueueAsMuchAsPossible (aliased as flushModuleScopingQueue) from @angular/core inside a beforeAll hook. Additionally, a comprehensive integration test has been added to verify this behavior. There are no review comments, and I have no additional feedback to provide.
…reAll In JIT mode (`aot: false`), `@NgModule` enqueues its module onto Angular's delayed scoping queue once when the class is evaluated. `TestBed` only flushes that queue once per realm (`checkGlobalCompilationFinished`), assuming all spec files were evaluated before the first test. With `@angular/build:unit-test` running Vitest in non-isolated mode (`isolate: false` by default), spec files are imported sequentially into the same worker. If a later spec file imports an `@NgModule` without compiling any component, `TestBed.resetTestingModule()` clears the pending scoping queue via `resetCompiledComponents()`, permanently losing the module's scope for subsequent specs (`NG0303` / `NG0302`). Registering `beforeAll(() => ɵflushModuleScopingQueueAsMuchAsPossible())` in the Vitest TestBed setup file flushes file-scope `@NgModule`s right after each spec file's imports are evaluated and before any `beforeEach` or `it` runs, without affecting test-local `@NgModule`s or incurring work in AOT mode. Fixes angular/angular#72266
JeanMeche
force-pushed
the
fix/vitest-jit-ngmodule-scope
branch
from
December 1, 2026 21:18
494b233 to
2e6b08d
Compare
alan-agius4
approved these changes
Dec 2, 2026
Collaborator
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.
Summary
In JIT mode (
aot: false),@NgModuleenqueues its module onto Angular's delayed scoping queue once when the class is evaluated.TestBedonly flushes that queue once per realm (checkGlobalCompilationFinished), assuming all spec files were evaluated before the first test.With
@angular/build:unit-testrunning Vitest in non-isolated mode (isolate: falseby default), spec files are imported sequentially into the same worker. If a later spec file imports an@NgModulewithout compiling any component,TestBed.resetTestingModule()clears the pending scoping queue viaresetCompiledComponents(), permanently losing the module's scope for subsequent specs (NG0303/NG0302).This registers
beforeAll(() => ɵflushModuleScopingQueueAsMuchAsPossible())in the Vitest TestBed setup virtual file so file-scope@NgModules are flushed right after each spec file's imports are evaluated and before anybeforeEach/itruns — without affecting test-local@NgModules declared insideit(...)or doing work in AOT mode.Fixes angular/angular#72266