From d277ee9ae914c3a33c5354c2df4c707f321e2a4f Mon Sep 17 00:00:00 2001
From: nagahara-naoki <118710021+nagahara-naoki@users.noreply.github.com>
Date: Sat, 7 Nov 2026 23:41:13 +0900
Subject: [PATCH 1/2] fix(@angular/build): preserve component initialization in
Karma test bundles
Load selected Karma specs through a shared virtual module while retaining spec entry names and shared framework chunks. Add a browser regression for barrel imports combined with async specs.
Fixes #34250
---
.../src/builders/karma/application_builder.ts | 24 ++++-
.../tests/behavior/barrel-import_spec.ts | 94 +++++++++++++++++++
2 files changed, 117 insertions(+), 1 deletion(-)
create mode 100644 packages/angular/build/src/builders/karma/tests/behavior/barrel-import_spec.ts
diff --git a/packages/angular/build/src/builders/karma/application_builder.ts b/packages/angular/build/src/builders/karma/application_builder.ts
index 416e14fab906..eef4eba67672 100644
--- a/packages/angular/build/src/builders/karma/application_builder.ts
+++ b/packages/angular/build/src/builders/karma/application_builder.ts
@@ -39,6 +39,7 @@ import type { KarmaBuilderTransformsOptions } from './index';
interface BuildOptions extends ApplicationBuilderInternalOptions {
// We know that it's always a string since we set it.
outputPath: string;
+ entryPoints: Map;
}
class ApplicationBuildError extends Error {
@@ -213,6 +214,25 @@ async function runEsbuild(
context: BuilderContext,
projectSourceRoot: string,
): Promise<[Result & { kind: ResultKind.Full }, AsyncIterator | null]> {
+ // Keep specs in a single module graph so cross-chunk lazy initialization cannot
+ // expose an uninitialized barrel export. Preserve entry names and shared framework chunks.
+ const specFiles: string[] = [];
+ const entryPoints = buildOptions.entryPoints;
+ for (const [name, file] of entryPoints) {
+ if (name.startsWith('spec-')) {
+ specFiles.push(file);
+ entryPoints.set(name, 'angular:karma-specs');
+ }
+ }
+ buildOptions.rootFiles = specFiles;
+ const virtualSpecs = createVirtualModulePlugin({
+ namespace: 'angular:karma-specs',
+ loadContent: () => ({
+ contents: specFiles.map((file) => `import ${JSON.stringify(file)};`).join('\n'),
+ loader: 'js',
+ resolveDir: projectSourceRoot,
+ }),
+ });
const usesZoneJS = buildOptions.polyfills?.includes('zone.js');
let hasLocalize = false;
try {
@@ -248,7 +268,9 @@ async function runEsbuild(
// Build tests with `application` builder, using test files as entry points.
const [buildOutput, buildIterator] = await first(
- buildApplicationInternal(buildOptions, context, { codePlugins: [virtualTestBedInit] }),
+ buildApplicationInternal(buildOptions, context, {
+ codePlugins: [virtualTestBedInit, virtualSpecs],
+ }),
{ cancel: !buildOptions.watch },
);
if (buildOutput.kind === ResultKind.Failure) {
diff --git a/packages/angular/build/src/builders/karma/tests/behavior/barrel-import_spec.ts b/packages/angular/build/src/builders/karma/tests/behavior/barrel-import_spec.ts
new file mode 100644
index 000000000000..8435630a19d6
--- /dev/null
+++ b/packages/angular/build/src/builders/karma/tests/behavior/barrel-import_spec.ts
@@ -0,0 +1,94 @@
+/**
+ * @license
+ * Copyright Google LLC All Rights Reserved.
+ *
+ * Use of this source code is governed by an MIT-style license that can be
+ * found in the LICENSE file at https://angular.dev/license
+ */
+
+import { execute } from '../../index';
+import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeKarmaBuilder } from '../setup';
+
+describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => {
+ describe('Behavior: barrel imports with async tests', () => {
+ beforeEach(async () => {
+ await setupTarget(harness);
+ });
+ it('should initialize components imported through a barrel', async () => {
+ harness.useTarget('test', { ...BASE_OPTIONS, include: ['src/app/repro/**/*.spec.ts'] });
+ await harness.writeFiles({
+ 'src/app/repro/layout/layout.ts': `
+import { Component } from '@angular/core';
+@Component({ selector: 'app-layout', template: '' })
+export class Layout {}
+`,
+ 'src/app/repro/layout/index.ts': `
+export * from './layout';
+`,
+ 'src/app/repro/a/dialog.ts': `
+import { Component } from '@angular/core';
+import { Layout } from '../layout';
+@Component({
+ selector: 'app-dialog',
+ imports: [Layout],
+ template: 'dialog',
+})
+export class Dialog {}
+`,
+ 'src/app/repro/c/page.ts': `
+import { Component } from '@angular/core';
+import { Layout } from '../layout';
+@Component({
+ selector: 'app-page',
+ imports: [Layout],
+ template: 'page',
+})
+export class Page {}
+`,
+ 'src/app/repro/a/dialog.spec.ts': `
+import { TestBed } from '@angular/core/testing';
+import { Dialog } from './dialog';
+describe('a/dialog.spec.ts', () => {
+ it('creates', () => {
+ TestBed.configureTestingModule({ imports: [Dialog] });
+ expect(TestBed.createComponent(Dialog).componentInstance).toBeTruthy();
+ });
+});
+`,
+ 'src/app/repro/a/dialog-again.spec.ts': `
+import { TestBed } from '@angular/core/testing';
+import { Dialog } from './dialog';
+describe('a/dialog-again.spec.ts', () => {
+ it('creates', () => {
+ TestBed.configureTestingModule({ imports: [Dialog] });
+ expect(TestBed.createComponent(Dialog).componentInstance).toBeTruthy();
+ });
+});
+`,
+ 'src/app/repro/c/page.spec.ts': `
+import { TestBed } from '@angular/core/testing';
+import { Page } from './page';
+describe('c/page.spec.ts', () => {
+ it('creates', () => {
+ TestBed.configureTestingModule({ imports: [Page] });
+ expect(TestBed.createComponent(Page).componentInstance).toBeTruthy();
+ });
+});
+`,
+ 'src/app/repro/b/other.spec.ts': `
+import { TestBed } from '@angular/core/testing';
+import { Layout } from '../layout/layout';
+describe('async spec', () => {
+ it('creates', async () => {
+ TestBed.configureTestingModule({ imports: [Layout] });
+ await Promise.resolve();
+ expect(TestBed.createComponent(Layout).componentInstance).toBeTruthy();
+ });
+});
+`,
+ });
+ const { result } = await harness.executeOnce();
+ expect(result?.success).toBeTrue();
+ });
+ });
+});
From bbcf1b76c0f70a812cdd250b780f492e74e8a92e Mon Sep 17 00:00:00 2001
From: nagahara-naoki <118710021+nagahara-naoki@users.noreply.github.com>
Date: Sun, 8 Nov 2026 00:22:28 +0900
Subject: [PATCH 2/2] fixup! fix(@angular/build): preserve component
initialization in Karma test bundles
---
.../src/builders/karma/application_builder.ts | 2 +-
.../tests/behavior/barrel-import_spec.ts | 131 +++++++++---------
2 files changed, 70 insertions(+), 63 deletions(-)
diff --git a/packages/angular/build/src/builders/karma/application_builder.ts b/packages/angular/build/src/builders/karma/application_builder.ts
index eef4eba67672..b0a780149182 100644
--- a/packages/angular/build/src/builders/karma/application_builder.ts
+++ b/packages/angular/build/src/builders/karma/application_builder.ts
@@ -224,7 +224,7 @@ async function runEsbuild(
entryPoints.set(name, 'angular:karma-specs');
}
}
- buildOptions.rootFiles = specFiles;
+ buildOptions.rootFiles = [...(buildOptions.rootFiles ?? []), ...specFiles];
const virtualSpecs = createVirtualModulePlugin({
namespace: 'angular:karma-specs',
loadContent: () => ({
diff --git a/packages/angular/build/src/builders/karma/tests/behavior/barrel-import_spec.ts b/packages/angular/build/src/builders/karma/tests/behavior/barrel-import_spec.ts
index 8435630a19d6..492a72dd1cdf 100644
--- a/packages/angular/build/src/builders/karma/tests/behavior/barrel-import_spec.ts
+++ b/packages/angular/build/src/builders/karma/tests/behavior/barrel-import_spec.ts
@@ -14,80 +14,87 @@ describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => {
beforeEach(async () => {
await setupTarget(harness);
});
+
it('should initialize components imported through a barrel', async () => {
- harness.useTarget('test', { ...BASE_OPTIONS, include: ['src/app/repro/**/*.spec.ts'] });
+ harness.useTarget('test', {
+ ...BASE_OPTIONS,
+ include: ['src/app/repro/**/*.spec.ts'],
+ });
+
await harness.writeFiles({
'src/app/repro/layout/layout.ts': `
-import { Component } from '@angular/core';
-@Component({ selector: 'app-layout', template: '' })
-export class Layout {}
-`,
+ import { Component } from '@angular/core';
+ @Component({ selector: 'app-layout', template: '' })
+ export class Layout {}
+ `,
'src/app/repro/layout/index.ts': `
-export * from './layout';
-`,
+ export * from './layout';
+ `,
'src/app/repro/a/dialog.ts': `
-import { Component } from '@angular/core';
-import { Layout } from '../layout';
-@Component({
- selector: 'app-dialog',
- imports: [Layout],
- template: 'dialog',
-})
-export class Dialog {}
-`,
+ import { Component } from '@angular/core';
+ import { Layout } from '../layout';
+ @Component({
+ selector: 'app-dialog',
+ imports: [Layout],
+ template: 'dialog',
+ })
+ export class Dialog {}
+ `,
'src/app/repro/c/page.ts': `
-import { Component } from '@angular/core';
-import { Layout } from '../layout';
-@Component({
- selector: 'app-page',
- imports: [Layout],
- template: 'page',
-})
-export class Page {}
-`,
+ import { Component } from '@angular/core';
+ import { Layout } from '../layout';
+ @Component({
+ selector: 'app-page',
+ imports: [Layout],
+ template: 'page',
+ })
+ export class Page {}
+ `,
'src/app/repro/a/dialog.spec.ts': `
-import { TestBed } from '@angular/core/testing';
-import { Dialog } from './dialog';
-describe('a/dialog.spec.ts', () => {
- it('creates', () => {
- TestBed.configureTestingModule({ imports: [Dialog] });
- expect(TestBed.createComponent(Dialog).componentInstance).toBeTruthy();
- });
-});
-`,
+ import { TestBed } from '@angular/core/testing';
+ import { Dialog } from './dialog';
+ describe('a/dialog.spec.ts', () => {
+ it('creates', () => {
+ TestBed.configureTestingModule({ imports: [Dialog] });
+ expect(TestBed.createComponent(Dialog).componentInstance).toBeTruthy();
+ });
+ });
+ `,
'src/app/repro/a/dialog-again.spec.ts': `
-import { TestBed } from '@angular/core/testing';
-import { Dialog } from './dialog';
-describe('a/dialog-again.spec.ts', () => {
- it('creates', () => {
- TestBed.configureTestingModule({ imports: [Dialog] });
- expect(TestBed.createComponent(Dialog).componentInstance).toBeTruthy();
- });
-});
-`,
+ import { TestBed } from '@angular/core/testing';
+ import { Dialog } from './dialog';
+ describe('a/dialog-again.spec.ts', () => {
+ it('creates', () => {
+ TestBed.configureTestingModule({ imports: [Dialog] });
+ expect(TestBed.createComponent(Dialog).componentInstance).toBeTruthy();
+ });
+ });
+ `,
'src/app/repro/c/page.spec.ts': `
-import { TestBed } from '@angular/core/testing';
-import { Page } from './page';
-describe('c/page.spec.ts', () => {
- it('creates', () => {
- TestBed.configureTestingModule({ imports: [Page] });
- expect(TestBed.createComponent(Page).componentInstance).toBeTruthy();
- });
-});
-`,
+ import { TestBed } from '@angular/core/testing';
+ import { Page } from './page';
+ describe('c/page.spec.ts', () => {
+ it('creates', () => {
+ TestBed.configureTestingModule({ imports: [Page] });
+ expect(TestBed.createComponent(Page).componentInstance).toBeTruthy();
+ });
+ });
+ `,
'src/app/repro/b/other.spec.ts': `
-import { TestBed } from '@angular/core/testing';
-import { Layout } from '../layout/layout';
-describe('async spec', () => {
- it('creates', async () => {
- TestBed.configureTestingModule({ imports: [Layout] });
- await Promise.resolve();
- expect(TestBed.createComponent(Layout).componentInstance).toBeTruthy();
- });
-});
-`,
+ import { TestBed } from '@angular/core/testing';
+ import { Layout } from '../layout/layout';
+ describe('async spec', () => {
+ it('creates', async () => {
+ TestBed.configureTestingModule({ imports: [Layout] });
+ await Promise.resolve();
+ expect(TestBed.createComponent(Layout).componentInstance).toBeTruthy();
+ });
+ });
+ `,
});
+
const { result } = await harness.executeOnce();
+
expect(result?.success).toBeTrue();
});
});