Skip to content

Commit 7f1e8c6

Browse files
committed
fix(@angular/build): include component test metadata in development builds
To support the usage of AOT with unit tests, development builds of applications will now include injected calls to the internal Angular `setClassMetadata` function. These calls add metadata to each component that can be leveraged by the `TestBed` to override component information. (cherry picked from commit 6f913ad)
1 parent f3d6159 commit 7f1e8c6

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 { buildApplication } from '../../index';
10+
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
11+
12+
describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
13+
describe('Option: "optimization.scripts"', () => {
14+
it(`should include 'setClassMetadata' calls when false`, async () => {
15+
harness.useTarget('build', {
16+
...BASE_OPTIONS,
17+
optimization: {
18+
scripts: false,
19+
},
20+
});
21+
22+
const { result } = await harness.executeOnce();
23+
expect(result?.success).toBeTrue();
24+
25+
harness.expectFile('dist/browser/main.js').content.toContain('setClassMetadata(');
26+
});
27+
28+
it(`should not include 'setClassMetadata' calls when true`, async () => {
29+
harness.useTarget('build', {
30+
...BASE_OPTIONS,
31+
optimization: {
32+
scripts: true,
33+
},
34+
});
35+
36+
const { result } = await harness.executeOnce();
37+
expect(result?.success).toBeTrue();
38+
39+
harness.expectFile('dist/browser/main.js').content.not.toContain('setClassMetadata(');
40+
});
41+
});
42+
});

Diff for: packages/angular/build/src/tools/esbuild/angular/compiler-plugin.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface CompilerPluginOptions {
3535
sourcemap: boolean | 'external';
3636
tsconfig: string;
3737
jit?: boolean;
38+
includeTestMetadata?: boolean;
3839

3940
advancedOptimizations?: boolean;
4041
thirdPartySourcemaps?: boolean;
@@ -292,7 +293,6 @@ export function createCompilerPlugin(
292293
pluginOptions,
293294
preserveSymlinks,
294295
build.initialOptions.conditions,
295-
build.initialOptions.absWorkingDir,
296296
),
297297
);
298298
shouldTsIgnoreJs = !initializationResult.compilerOptions.allowJs;
@@ -623,7 +623,6 @@ function createCompilerOptionsTransformer(
623623
pluginOptions: CompilerPluginOptions,
624624
preserveSymlinks: boolean | undefined,
625625
customConditions: string[] | undefined,
626-
absWorkingDir: string | undefined,
627626
): Parameters<AngularCompilation['initialize']>[2] {
628627
return (compilerOptions) => {
629628
// target of 9 is ES2022 (using the number avoids an expensive import of typescript just for an enum)
@@ -714,6 +713,7 @@ function createCompilerOptionsTransformer(
714713
preserveSymlinks,
715714
externalRuntimeStyles: pluginOptions.externalRuntimeStyles,
716715
_enableHmr: !!pluginOptions.templateUpdates,
716+
supportTestBed: !!pluginOptions.includeTestMetadata,
717717
};
718718
};
719719
}

Diff for: packages/angular/build/src/tools/esbuild/compiler-plugin-options.ts

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export function createCompilerPluginOptions(
2727
jit,
2828
externalRuntimeStyles,
2929
instrumentForCoverage,
30+
optimizationOptions,
3031
} = options;
3132
const incremental = !!options.watch;
3233

@@ -43,5 +44,6 @@ export function createCompilerPluginOptions(
4344
externalRuntimeStyles,
4445
instrumentForCoverage,
4546
templateUpdates,
47+
includeTestMetadata: !optimizationOptions.scripts,
4648
};
4749
}

0 commit comments

Comments
 (0)