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 @@ -110,5 +110,172 @@ describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});

it('should not compile unimported application files when testing standalone code', async () => {
await harness.writeFiles({
'src/tsconfig.spec.json': JSON.stringify({
extends: '../tsconfig.json',
compilerOptions: {
outDir: '../out-tsc/spec',
types: ['jasmine', '@angular/localize'],
},
files: ['polyfills.ts'],
include: ['**/*.ts', '**/*.d.ts'],
}),
'src/app/services/test.service.spec.ts': `
describe('TestService', () => {
it('should succeed', () => {
expect(true).toBe(true);
});
});`,
'src/app/unused-broken.ts': `
// This unimported file has a TypeScript error and should not be compiled for standalone tests
export const invalidNumber: number = 'not a number';
`,
});

harness.useTarget('test', {
...BASE_OPTIONS,
include: ['src/app/services/test.service.spec.ts'],
});

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

it('should resolve NgModule scope when testing a non-standalone component without importing the NgModule', async () => {
await harness.writeFiles({
'src/tsconfig.spec.json': JSON.stringify({
extends: '../tsconfig.json',
compilerOptions: {
outDir: '../out-tsc/spec',
types: ['jasmine', '@angular/localize'],
},
files: ['polyfills.ts'],
include: ['**/*.ts', '**/*.d.ts'],
}),
'src/app/app.ts': `
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: '<h1>Hello, {{ title }}</h1><router-outlet />',
standalone: false,
})
export class App {
title = 'test';
}
`,
'src/app/app-module.ts': `
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { App } from './app';

@NgModule({
declarations: [App],
imports: [BrowserModule, RouterModule.forRoot([])],
bootstrap: [App],
})
export class AppModule {}
`,
'src/app/app.spec.ts': `
import { TestBed } from '@angular/core/testing';
import { RouterModule } from '@angular/router';
import { App } from './app';

describe('App', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RouterModule.forRoot([])],
declarations: [App],
}).compileComponents();
});

it('should create the app', () => {
const fixture = TestBed.createComponent(App);
expect(fixture.componentInstance).toBeTruthy();
});
});
`,
});

harness.useTarget('test', {
...BASE_OPTIONS,
include: ['src/app/app.spec.ts'],
});

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

it('should not compile unimported files when a non-standalone component NgModule is imported by the test', async () => {
await harness.writeFiles({
'src/tsconfig.spec.json': JSON.stringify({
extends: '../tsconfig.json',
compilerOptions: {
outDir: '../out-tsc/spec',
types: ['jasmine', '@angular/localize'],
},
files: ['polyfills.ts'],
include: ['**/*.ts', '**/*.d.ts'],
}),
'src/app/app.ts': `
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: '<h1>Hello, {{ title }}</h1><router-outlet />',
standalone: false,
})
export class App {
title = 'test';
}
`,
'src/app/app-module.ts': `
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { App } from './app';

@NgModule({
declarations: [App],
imports: [BrowserModule, RouterModule.forRoot([])],
bootstrap: [App],
})
export class AppModule {}
`,
'src/app/app.spec.ts': `
import { TestBed } from '@angular/core/testing';
import { App } from './app';
import { AppModule } from './app-module';

describe('App', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [AppModule],
}).compileComponents();
});

it('should create the app', () => {
const fixture = TestBed.createComponent(App);
expect(fixture.componentInstance).toBeTruthy();
});
});
`,
'src/app/unused-broken.ts': `
// This unimported file has a TypeScript error and should not be compiled when AppModule is already imported
export const invalidNumber: number = 'not a number';
`,
});

harness.useTarget('test', {
...BASE_OPTIONS,
include: ['src/app/app.spec.ts'],
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ describe('AngularCompilation', () => {
return this.sourceFiles;
}

public async testLoadConfiguration(tsconfig: string, overrides?: CompilerOptionOverrides) {
return this.loadConfiguration(tsconfig, overrides);
public async testLoadConfiguration(
tsconfig: string,
overrides?: CompilerOptionOverrides,
buildType?: 'application' | 'library',
) {
return this.loadConfiguration(tsconfig, overrides, buildType);
}
}

Expand Down Expand Up @@ -162,12 +166,83 @@ describe('AngularCompilation', () => {
jasmine.any(Map),
);
expect(result.rootNames).toEqual(['/src/main.ts']);
expect(result.fallbackRootNames).toBeUndefined();
expect(result.compilerOptions.target).toBe(ts.ScriptTarget.ES2022);
expect(result.compilerOptions.inlineSources).toBe(true);
expect(result.warnings.length).toBeGreaterThan(0);
expect(result.tsConfigFiles).toEqual(['tsconfig.json']);
});

it('filters rootNames to rootFiles while retaining ambient declaration files and setting fallbackRootNames', async () => {
const compilation = new MockTypeScriptCompilation();
const mockReadConfig = jasmine.createSpy('readConfiguration').and.returnValue({
options: { target: ts.ScriptTarget.ES2022 },
rootNames: [
'/src/app/included.spec.ts',
'/src/app/excluded.spec.ts',
'/src/app/app-module.ts',
'/src/env.d.ts',
'/src/types.d.mts',
'/src/legacy.d.cts',
],
errors: [],
});
spyOn(TypeScriptCompilation, 'loadCompilerCli').and.resolveTo({
readConfiguration: mockReadConfig,
} as unknown as typeof import('@angular/compiler-cli'));

const result = await compilation.testLoadConfiguration('tsconfig.spec.json', {
rootFiles: ['/src/app/included.spec.ts'],
});

expect(result.rootNames).toEqual([
'/src/app/included.spec.ts',
'/src/env.d.ts',
'/src/types.d.mts',
'/src/legacy.d.cts',
]);
expect(result.fallbackRootNames).toEqual([
'/src/app/included.spec.ts',
'/src/app/excluded.spec.ts',
'/src/app/app-module.ts',
'/src/env.d.ts',
'/src/types.d.mts',
'/src/legacy.d.cts',
]);
});

it('omits fallbackRootNames when buildType is "library" or when rootFiles covers all tsconfig files', async () => {
const compilation = new MockTypeScriptCompilation();
const mockReadConfig = jasmine.createSpy('readConfiguration').and.returnValue({
options: { target: ts.ScriptTarget.ES2022 },
rootNames: ['/src/public-api.ts', '/src/internal.ts', '/src/env.d.ts'],
errors: [],
});
spyOn(TypeScriptCompilation, 'loadCompilerCli').and.resolveTo({
readConfiguration: mockReadConfig,
} as unknown as typeof import('@angular/compiler-cli'));

const libraryResult = await compilation.testLoadConfiguration(
'tsconfig.lib.json',
{ rootFiles: ['/src/public-api.ts'] },
'library',
);
expect(libraryResult.rootNames).toEqual(['/src/public-api.ts', '/src/env.d.ts']);
expect(libraryResult.fallbackRootNames).toBeUndefined();

const fullSetResult = await compilation.testLoadConfiguration(
'tsconfig.app.json',
{ rootFiles: ['/src/public-api.ts', '/src/internal.ts'] },
'application',
);
expect(fullSetResult.rootNames).toEqual([
'/src/public-api.ts',
'/src/internal.ts',
'/src/env.d.ts',
]);
expect(fullSetResult.fallbackRootNames).toBeUndefined();
});

it('passes extendedConfigCache to readConfiguration and invalidates entries on config change', async () => {
const compilation = new MockTypeScriptCompilation();
let passedCache: Map<string, ts.ExtendedConfigCacheEntry> | undefined;
Expand Down
Loading
Loading