diff --git a/packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts b/packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts index efff72e5d8bb..e2bfcea3270e 100644 --- a/packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts +++ b/packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts @@ -49,16 +49,22 @@ function createTestBedInitVirtualFile( return ` ${hasLocalize ? "import '@angular/localize/init';" : ''} // Initialize the Angular testing environment - import { NgModule, provideZoneChangeDetection } from '@angular/core'; + import { + NgModule, + provideZoneChangeDetection, + ɵflushModuleScopingQueueAsMuchAsPossible as flushModuleScopingQueue, + } from '@angular/core'; import { getTestBed, ɵgetCleanupHook as getCleanupHook, TestComponentRenderer } from '@angular/core/testing'; import { BrowserTestingModule, platformBrowserTesting } from '@angular/platform-browser/testing'; import { ɵgetDOM } from '@angular/common'; - import { afterEach, beforeEach } from 'vitest'; + import { afterEach, beforeAll, beforeEach } from 'vitest'; ${providersImport} - // The beforeEach and afterEach hooks are registered outside the globalThis guard. + // The beforeAll, beforeEach, and afterEach hooks are registered outside the globalThis guard. // This ensures that the hooks are always applied, even in non-isolated browser environments. // Same as https://github.com/angular/angular/blob/05a03d3f975771bb59c7eefd37c01fa127ee2229/packages/core/testing/srcs/test_hooks.ts#L21-L29 + // Flush pending JIT NgModule scopes evaluated when each spec file is imported in a shared worker. + beforeAll(() => flushModuleScopingQueue()); beforeEach(getCleanupHook(false)); afterEach(getCleanupHook(true)); diff --git a/packages/angular/build/src/builders/unit-test/tests/options/isolate_spec.ts b/packages/angular/build/src/builders/unit-test/tests/options/isolate_spec.ts index 28fbb855a91f..ea8836da5083 100644 --- a/packages/angular/build/src/builders/unit-test/tests/options/isolate_spec.ts +++ b/packages/angular/build/src/builders/unit-test/tests/options/isolate_spec.ts @@ -108,5 +108,134 @@ describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => { const { result } = await harness.executeOnce(); expect(result?.success).toBeTrue(); }); + + it('should preserve JIT NgModule scopes across spec files when isolate is false', async () => { + setupApplicationTarget(harness, { aot: false }); + + harness.writeFile( + 'vitest-base.config.mts', + ` + import { defineConfig } from 'vitest/config'; + import { BaseSequencer, type TestSpecification } from 'vitest/node'; + + class FileNameSequencer extends BaseSequencer { + override async sort(files: TestSpecification[]) { + return [...files].sort((a, b) => a.moduleId.localeCompare(b.moduleId)); + } + } + + export default defineConfig({ + test: { + fileParallelism: false, + sequence: { sequencer: FileNameSequencer }, + }, + }); + `, + ); + + harness.writeFile( + 'src/app/legacy.ts', + ` + import { CommonModule } from '@angular/common'; + import { Component, Injectable, NgModule, input } from '@angular/core'; + + @Component({ + selector: 'app-legacy-list', + standalone: false, + template: '