Skip to content

Commit 9d7613d

Browse files
committed
fix(@angular-devkit/build-angular): zone.js/testing + karma + esbuild
Previously, the testing module was split into its own entrypoint but then never loaded. Now it's just left in the overall polyfill bundle. The bug wasn't caught by the existing test coverage, so this adds a new test that ensures that fakeAsync works. Cleaning up the Karma `files` list also removes the noisy "no file matched the pattern worker-*.js" warnings that were previously generated for test suites that don't include web worker sources.
1 parent 87a90af commit 9d7613d

File tree

2 files changed

+93
-30
lines changed

2 files changed

+93
-30
lines changed

packages/angular_devkit/build_angular/src/builders/karma/application_builder.ts

+16-30
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,19 @@ async function getProjectSourceRoot(context: BuilderContext): Promise<string> {
8585
return path.join(context.workspaceRoot, sourceRoot);
8686
}
8787

88+
function normalizePolyfills(polyfills: string | string[] | undefined): string[] {
89+
if (typeof polyfills === 'string') {
90+
return [polyfills];
91+
}
92+
93+
return polyfills ?? [];
94+
}
95+
8896
async function collectEntrypoints(
8997
options: KarmaBuilderOptions,
9098
context: BuilderContext,
9199
projectSourceRoot: string,
92-
): Promise<[Set<string>, string[]]> {
100+
): Promise<Set<string>> {
93101
// Glob for files to test.
94102
const testFiles = await findTests(
95103
options.include ?? [],
@@ -102,13 +110,8 @@ async function collectEntrypoints(
102110
...testFiles,
103111
'@angular-devkit/build-angular/src/builders/karma/init_test_bed.js',
104112
]);
105-
// Extract `zone.js/testing` to a separate entry point because it needs to be loaded after Jasmine.
106-
const [polyfills, hasZoneTesting] = extractZoneTesting(options.polyfills);
107-
if (hasZoneTesting) {
108-
entryPoints.add('zone.js/testing');
109-
}
110113

111-
return [entryPoints, polyfills];
114+
return entryPoints;
112115
}
113116

114117
async function initializeApplication(
@@ -129,7 +132,7 @@ async function initializeApplication(
129132
const testDir = path.join(context.workspaceRoot, 'dist/test-out', randomUUID());
130133
const projectSourceRoot = await getProjectSourceRoot(context);
131134

132-
const [karma, [entryPoints, polyfills]] = await Promise.all([
135+
const [karma, entryPoints] = await Promise.all([
133136
import('karma'),
134137
collectEntrypoints(options, context, projectSourceRoot),
135138
fs.rm(testDir, { recursive: true, force: true }),
@@ -162,7 +165,7 @@ async function initializeApplication(
162165
},
163166
instrumentForCoverage,
164167
styles: options.styles,
165-
polyfills,
168+
polyfills: normalizePolyfills(options.polyfills),
166169
webWorkerTsConfig: options.webWorkerTsConfig,
167170
},
168171
context,
@@ -184,11 +187,10 @@ async function initializeApplication(
184187
// Serve polyfills first.
185188
{ pattern: `${testDir}/polyfills.js`, type: 'module' },
186189
// Allow loading of chunk-* files but don't include them all on load.
187-
{ pattern: `${testDir}/chunk-*.js`, type: 'module', included: false },
188-
// Allow loading of worker-* files but don't include them all on load.
189-
{ pattern: `${testDir}/worker-*.js`, type: 'module', included: false },
190-
// `zone.js/testing`, served but not included on page load.
191-
{ pattern: `${testDir}/testing.js`, type: 'module', included: false },
190+
{ pattern: `${testDir}/{chunk,worker}-*.js`, type: 'module', included: false },
191+
);
192+
193+
karmaOptions.files.push(
192194
// Serve remaining JS on page load, these are the test entrypoints.
193195
{ pattern: `${testDir}/*.js`, type: 'module' },
194196
);
@@ -266,22 +268,6 @@ export async function writeTestFiles(files: Record<string, ResultFile>, testDir:
266268
});
267269
}
268270

269-
function extractZoneTesting(
270-
polyfills: readonly string[] | string | undefined,
271-
): [polyfills: string[], hasZoneTesting: boolean] {
272-
if (typeof polyfills === 'string') {
273-
polyfills = [polyfills];
274-
}
275-
polyfills ??= [];
276-
277-
const polyfillsWithoutZoneTesting = polyfills.filter(
278-
(polyfill) => polyfill !== 'zone.js/testing',
279-
);
280-
const hasZoneTesting = polyfills.length !== polyfillsWithoutZoneTesting.length;
281-
282-
return [polyfillsWithoutZoneTesting, hasZoneTesting];
283-
}
284-
285271
/** Returns the first item yielded by the given generator and cancels the execution. */
286272
async function first<T>(generator: AsyncIterable<T>): Promise<T> {
287273
for await (const value of generator) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { execute } from '../../index';
10+
import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeKarmaBuilder } from '../setup';
11+
12+
describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget, isApp) => {
13+
describe('Behavior: "fakeAsync"', () => {
14+
beforeEach(async () => {
15+
await setupTarget(harness);
16+
});
17+
18+
it('loads zone.js/testing at the right time', async () => {
19+
await harness.writeFiles({
20+
'./src/app/app.component.ts': `
21+
import { Component } from '@angular/core';
22+
23+
@Component({
24+
selector: 'app-root',
25+
template: '<button (click)="changeMessage()" class="change">{{ message }}</button>',
26+
})
27+
export class AppComponent {
28+
message = 'Initial';
29+
30+
changeMessage() {
31+
setTimeout(() => {
32+
this.message = 'Changed';
33+
}, 1000);
34+
}
35+
}`,
36+
'./src/app/app.component.spec.ts': `
37+
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
38+
import { By } from '@angular/platform-browser';
39+
import { AppComponent } from './app.component';
40+
41+
describe('AppComponent', () => {
42+
beforeEach(() => TestBed.configureTestingModule({
43+
declarations: [AppComponent]
44+
}));
45+
46+
it('allows terrible things that break the most basic assumptions', fakeAsync(() => {
47+
const fixture = TestBed.createComponent(AppComponent);
48+
49+
const btn = fixture.debugElement
50+
.query(By.css('button.change'));
51+
52+
fixture.detectChanges();
53+
expect(btn.nativeElement.innerText).toBe('Initial');
54+
55+
btn.triggerEventHandler('click', null);
56+
57+
// Pre-tick: Still the old value.
58+
fixture.detectChanges();
59+
expect(btn.nativeElement.innerText).toBe('Initial');
60+
61+
tick(1500);
62+
63+
fixture.detectChanges();
64+
expect(btn.nativeElement.innerText).toBe('Changed');
65+
}));
66+
});`,
67+
});
68+
69+
harness.useTarget('test', {
70+
...BASE_OPTIONS,
71+
});
72+
73+
const { result } = await harness.executeOnce();
74+
expect(result?.success).toBeTrue();
75+
});
76+
});
77+
});

0 commit comments

Comments
 (0)