Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<ul><li *ngFor="let item of items" class="item">{{ item }}</li></ul>',
})
export class LegacyListComponent {
items = ['a', 'b'];
}

@NgModule({
imports: [CommonModule],
declarations: [LegacyListComponent],
exports: [LegacyListComponent],
})
export class LegacyModule {}

@Component({
selector: 'app-panel',
imports: [LegacyModule],
template: '<app-legacy-list />',
})
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();
});
});
});
Loading