Skip to content

Commit f263306

Browse files
committed
refactor(@angular/build): make buildTarget optional in unit-test builder
The `buildTarget` option in the `unit-test` builder is now optional to streamline the user configuration. If the `buildTarget` option is not provided, the builder now defaults to using the `build` target with the `development` configuration for the current project. This change significantly reduces the boilerplate configuration needed in `angular.json` for most projects, as the builder can now infer the correct build settings by convention. The builder's schema has been updated to reflect this change by removing `buildTarget` from the required properties and updating its description to document the new default behavior.
1 parent 3b7dabb commit f263306

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

goldens/public-api/angular/build/index.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export type NgPackagrBuilderOptions = {
216216
// @public
217217
export type UnitTestBuilderOptions = {
218218
browsers?: string[];
219-
buildTarget: string;
219+
buildTarget?: string;
220220
coverage?: boolean;
221221
coverageAll?: boolean;
222222
coverageExclude?: string[];

packages/angular/build/src/builders/unit-test/builder.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,22 @@ export async function* execute(
240240
// Get base build options from the buildTarget
241241
let buildTargetOptions: ApplicationBuilderInternalOptions;
242242
try {
243+
const builderName = await context.getBuilderNameForTarget(normalizedOptions.buildTarget);
244+
if (
245+
builderName !== '@angular/build:application' &&
246+
// TODO: Add comprehensive support for ng-packagr.
247+
builderName !== '@angular/build:ng-packagr'
248+
) {
249+
context.logger.warn(
250+
`The 'buildTarget' is configured to use '${builderName}', which is not supported. ` +
251+
`The 'unit-test' builder is designed to work with '@angular/build:application'. ` +
252+
'Unexpected behavior or build failures may occur.',
253+
);
254+
}
255+
243256
buildTargetOptions = (await context.validateOptions(
244257
await context.getTargetOptions(normalizedOptions.buildTarget),
245-
await context.getBuilderNameForTarget(normalizedOptions.buildTarget),
258+
builderName,
246259
)) as unknown as ApplicationBuilderInternalOptions;
247260
} catch (e) {
248261
assertIsError(e);
@@ -297,12 +310,9 @@ export async function* execute(
297310
...runnerBuildOptions,
298311
watch: normalizedOptions.watch,
299312
progress: normalizedOptions.buildProgress ?? buildTargetOptions.progress,
313+
...(normalizedOptions.tsConfig ? { tsConfig: normalizedOptions.tsConfig } : {}),
300314
} satisfies ApplicationBuilderInternalOptions;
301315

302-
if (normalizedOptions.tsConfig) {
303-
applicationBuildOptions.tsConfig = normalizedOptions.tsConfig;
304-
}
305-
306316
const dumpDirectory = normalizedOptions.dumpVirtualFiles
307317
? path.join(normalizedOptions.cacheOptions.path, 'unit-test', 'output-files')
308318
: undefined;

packages/angular/build/src/builders/unit-test/schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"properties": {
77
"buildTarget": {
88
"type": "string",
9-
"description": "Specifies the build target to use for the unit test build in the format `project:target[:configuration]`. You can also pass a comma-separated list of configurations. Example: `project:target:production,staging`.",
9+
"description": "Specifies the build target to use for the unit test build in the format `project:target[:configuration]`. This defaults to the `build` target of the current project with the `development` configuration. You can also pass a comma-separated list of configurations. Example: `project:target:production,staging`.",
1010
"pattern": "^[^:\\s]*:[^:\\s]*(:[^\\s]+)?$"
1111
},
1212
"tsConfig": {
@@ -262,5 +262,5 @@
262262
}
263263
},
264264
"additionalProperties": false,
265-
"required": ["buildTarget", "runner"]
265+
"required": ["runner"]
266266
}

0 commit comments

Comments
 (0)