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: '', + }) + export class LegacyListComponent { + items = ['a', 'b']; + } + + @NgModule({ + imports: [CommonModule], + declarations: [LegacyListComponent], + exports: [LegacyListComponent], + }) + export class LegacyModule {} + + @Component({ + selector: 'app-panel', + imports: [LegacyModule], + template: '', + }) + export class PanelComponent { + label = input(''); + } + + @Injectable({ providedIn: 'root' }) + export class PanelRegistryService { + isPanel(instance: unknown) { + return instance instanceof PanelComponent; + } + } + `, + ); + + harness.writeFile( + 'src/app/1-unrelated.spec.ts', + ` + import { Injectable } from '@angular/core'; + import { TestBed } from '@angular/core/testing'; + + @Injectable({ providedIn: 'root' }) + class UnrelatedService { + value = 42; + } + + describe('UnrelatedService', () => { + it('is injectable', () => { + expect(TestBed.inject(UnrelatedService).value).toBe(42); + }); + }); + `, + ); + + harness.writeFile( + 'src/app/2-registry.spec.ts', + ` + import { TestBed } from '@angular/core/testing'; + import { PanelRegistryService } from './legacy'; + + describe('PanelRegistryService', () => { + it('recognises non-panels', () => { + expect(TestBed.inject(PanelRegistryService).isPanel({})).toBe(false); + }); + }); + `, + ); + + harness.writeFile( + 'src/app/3-panel.spec.ts', + ` + import { TestBed } from '@angular/core/testing'; + import { PanelComponent } from './legacy'; + + describe('PanelComponent', () => { + it('renders items using NgModule scope', () => { + const fixture = TestBed.createComponent(PanelComponent); + fixture.detectChanges(); + expect(fixture.nativeElement.querySelectorAll('.item').length).toBe(2); + }); + }); + `, + ); + + harness.useTarget('test', { + ...BASE_OPTIONS, + runner: 'vitest' as any, + runnerConfig: true, + isolate: false, + include: [ + 'src/app/1-unrelated.spec.ts', + 'src/app/2-registry.spec.ts', + 'src/app/3-panel.spec.ts', + ], + }); + + const { result } = await harness.executeOnce(); + expect(result?.success).toBeTrue(); + }); }); });