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 @@ -230,6 +230,10 @@ export async function getVitestBuildOptions(
}
}

// The Angular compiler facade must be loaded in a dedicated setup file before TestBed initialization.
// This ensures the compiler facade is published before 'init-testbed' or any shared code-split chunks
// (such as those from a configured 'providersFile') are evaluated.
entryPoints.set('init-compiler', 'angular:compiler-init');
entryPoints.set('init-testbed', 'angular:test-bed-init');
entryPoints.set('vitest-mock-patch', 'angular:vitest-mock-patch');

Expand Down Expand Up @@ -326,6 +330,7 @@ export async function getVitestBuildOptions(
return {
buildOptions,
virtualFiles: {
'angular:compiler-init': "import '@angular/compiler';",
'angular:test-bed-init': testBedInitContents,
'angular:vitest-mock-patch': mockPatchContents,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@ export class VitestExecutor implements TestExecutor {
private prepareSetupFiles(): string[] {
const { setupFiles } = this.options;
// Add setup file entries for TestBed initialization and project polyfills
const testSetupFiles = ['init-testbed.js', 'vitest-mock-patch.js', ...setupFiles];
const testSetupFiles = [
'init-compiler.js',
'init-testbed.js',
'vitest-mock-patch.js',
...setupFiles,
];

// TODO: Provide additional result metadata to avoid needing to extract based on filename
if (this.buildResultFiles.has('polyfills.js')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,59 @@ describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});

it('should work with providers that inject services requiring JIT compilation', async () => {
await harness.writeFiles({
'src/test.service.ts': `
import { Injectable, inject } from '@angular/core';
import { Router } from '@angular/router';
@Injectable({ providedIn: 'root' })
export class TestService {
router = inject(Router);
}
`,
'src/test.providers.ts': `
import { TestService } from './test.service';
export default [TestService];
`,
'src/app/app.component.spec.ts': `
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { TestService } from '../test.service';
import { describe, expect, it } from 'vitest';
describe('AppComponent', () => {
it('should create the app and inject TestService', () => {
TestBed.configureTestingModule({
declarations: [AppComponent],
});
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
const testService = TestBed.inject(TestService);
expect(app).toBeTruthy();
expect(testService).toBeTruthy();
expect(testService.router).toBeTruthy();
});
});
`,
});

await harness.modifyFile('src/tsconfig.spec.json', (content) => {
const tsConfig = JSON.parse(content);
tsConfig.files ??= [];
tsConfig.files.push('test.service.ts', 'test.providers.ts');

return JSON.stringify(tsConfig);
});

harness.useTarget('test', {
...BASE_OPTIONS,
providersFile: 'src/test.providers.ts',
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});
});
});
Loading