Skip to content

fix(@angular/build): flush pending JIT NgModule scopes in Vitest beforeAll - #34815

Merged
alan-agius4 merged 1 commit into
angular:mainfrom
JeanMeche:fix/vitest-jit-ngmodule-scope
Dec 2, 2026
Merged

alan-agius4 merged 1 commit into
angular:mainfrom
JeanMeche:fix/vitest-jit-ngmodule-scope

Conversation

@JeanMeche

Copy link
Copy Markdown
Member

Summary

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

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 any beforeEach / it runs — without affecting test-local @NgModules declared inside it(...) or doing work in AOT mode.

Fixes angular/angular#72266

@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 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
JeanMeche force-pushed the fix/vitest-jit-ngmodule-scope branch from 494b233 to 2e6b08d Compare December 1, 2026 21:18
@alan-agius4 alan-agius4 added target: patch This PR is targeted for the next patch release action: merge The PR is ready for merge by the caretaker labels Dec 2, 2026
@alan-agius4
alan-agius4 merged commit 3f2f357 into angular:main Dec 2, 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:

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

2 participants