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');