From 4a1a29b3f91b2b1081f3b0c6eaad5581d8a49bb3 Mon Sep 17 00:00:00 2001 From: MeAkib Date: Sun, 18 Oct 2026 17:12:12 +0600 Subject: [PATCH] fix(@schematics/angular): skip `webWorkerTsConfig` for unit-test builder test targets The `web-worker` schematic added the `webWorkerTsConfig` option to the project's `test` target regardless of its builder. New applications use the `@angular/build:unit-test` builder, which does not have this option, so `ng test` failed schema validation with `Unknown option "webWorkerTsConfig"` after generating a web worker. The option is no longer added to `test` targets that use the unit-test builder, which reads build options from its `buildTarget`. Other test builders, such as Karma, still receive the option. Fixes #34385 --- .../schematics/angular/web-worker/index.ts | 4 +++- .../angular/web-worker/index_spec.ts | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/schematics/angular/web-worker/index.ts b/packages/schematics/angular/web-worker/index.ts index a19e2b714174..45e1f39ad34c 100644 --- a/packages/schematics/angular/web-worker/index.ts +++ b/packages/schematics/angular/web-worker/index.ts @@ -26,6 +26,7 @@ import { parseName } from '../utility/parse-name'; import { relativePathToWorkspaceRoot } from '../utility/paths'; import { createProjectSchematic } from '../utility/project'; import { buildDefaultPath, updateWorkspace } from '../utility/workspace'; +import { Builders } from '../utility/workspace-models'; import { Schema as WebWorkerOptions } from './schema'; function addSnippet(options: WebWorkerOptions): Rule { @@ -115,7 +116,8 @@ const webWorkerSchematic: RuleFactory = createProjectSchematic const workerConfigPath = join(root, 'tsconfig.worker.json'); (buildTarget.options ??= {}).webWorkerTsConfig ??= workerConfigPath; - if (testTarget) { + // The unit-test builder has no `webWorkerTsConfig` option and reads it from the build target. + if (testTarget && testTarget.builder !== Builders.BuildUnitTest) { (testTarget.options ??= {}).webWorkerTsConfig ??= workerConfigPath; } }), diff --git a/packages/schematics/angular/web-worker/index_spec.ts b/packages/schematics/angular/web-worker/index_spec.ts index c1ab48b8d196..a922a6d3cee6 100644 --- a/packages/schematics/angular/web-worker/index_spec.ts +++ b/packages/schematics/angular/web-worker/index_spec.ts @@ -9,6 +9,7 @@ import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; import { parse as parseJson } from 'jsonc-parser'; import { Schema as ApplicationOptions } from '../application/schema'; +import { Builders } from '../utility/workspace-models'; import { Schema as WorkspaceOptions } from '../workspace/schema'; import { Schema as WebWorkerOptions } from './schema'; @@ -68,6 +69,25 @@ describe('Web Worker Schematic', () => { ); }); + it('should not add the webWorkerTsConfig option to a unit-test builder test target', async () => { + const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree); + const { projects } = JSON.parse(tree.readContent('/angular.json')); + expect(projects.bar.architect.test.builder).toBe(Builders.BuildUnitTest); + expect(projects.bar.architect.test.options?.webWorkerTsConfig).toBeUndefined(); + }); + + it('should add the webWorkerTsConfig option to a karma builder test target', async () => { + const config = JSON.parse(appTree.readContent('/angular.json')); + config.projects.bar.architect.test = { builder: Builders.BuildKarma, options: {} }; + appTree.overwrite('/angular.json', JSON.stringify(config, undefined, 2)); + + const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree); + const { projects } = JSON.parse(tree.readContent('/angular.json')); + expect(projects.bar.architect.test.options.webWorkerTsConfig).toBe( + 'projects/bar/tsconfig.worker.json', + ); + }); + it('should add snippet to sibling file', async () => { const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree); const appComponent = tree.readContent('/projects/bar/src/app/app.ts');