FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(@angular/build): ensure the JIT compiler is available for Vitest tests that use providersFile. by mistrykaran91 · Pull Request #32304 · angular/angular-cli · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
    • options.ts
      • build-options.ts
16 changes: 14 additions & 2 deletions packages/angular/build/src/builders/unit-test/options.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ export async function normalizeOptions(
};
}

export function injectTestingPolyfills(polyfills: string[] = []): string[] {
return polyfills.includes('zone.js') ? [...polyfills, 'zone.js/testing'] : polyfills;
export function injectTestingPolyfills(
polyfills: string[] = [],
runner?: Runner,
hasProvidersFile?: boolean,
): string[] {
const testPolyfills = polyfills.includes('zone.js')
? [...polyfills, 'zone.js/testing']
: polyfills;

if (runner === Runner.Vitest && hasProvidersFile) {
testPolyfills.push('@angular/compiler');
}

return testPolyfills;
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { toPosixPath } from '../../../../utils/path';
import type { ApplicationBuilderInternalOptions } from '../../../application/options';
import { OutputHashing } from '../../../application/schema';
import { NormalizedUnitTestBuilderOptions, injectTestingPolyfills } from '../../options';
import { Runner } from '../../schema';
import { findTests, getTestEntrypoints } from '../../test-discovery';
import { RunnerOptions } from '../api';

Expand Down Expand Up @@ -130,7 +131,11 @@ export async function getVitestBuildOptions(
externalDependencies,
};

buildOptions.polyfills = injectTestingPolyfills(buildOptions.polyfills);
buildOptions.polyfills = injectTestingPolyfills(
buildOptions.polyfills,
Runner.Vitest,
!!providersFile,
);

const testBedInitContents = createTestBedInitVirtualFile(
providersFile,
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,61 @@ 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 () => {
// This test reproduces the issue from https://github.com/angular/angular-cli/issues/31993
// where using providersFile with a service that injects Router causes JIT compilation errors
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();
});
});
});

Back | FazBrowse Home | New Git URL