Skip to content

Commit 36d9880

Browse files
MeAkibclydin
authored andcommitted
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 (cherry picked from commit 41b54ec)
1 parent eec8dc0 commit 36d9880

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎packages/schematics/angular/web-worker/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { parseName } from '../utility/parse-name';
2626
import { relativePathToWorkspaceRoot } from '../utility/paths';
2727
import { createProjectSchematic } from '../utility/project';
2828
import { buildDefaultPath, updateWorkspace } from '../utility/workspace';
29+
import { Builders } from '../utility/workspace-models';
2930
import { Schema as WebWorkerOptions } from './schema';
3031

3132
function addSnippet(options: WebWorkerOptions): Rule {
@@ -115,7 +116,8 @@ const webWorkerSchematic: RuleFactory<WebWorkerOptions> = createProjectSchematic
115116

116117
const workerConfigPath = join(root, 'tsconfig.worker.json');
117118
(buildTarget.options ??= {}).webWorkerTsConfig ??= workerConfigPath;
118-
if (testTarget) {
119+
// The unit-test builder has no `webWorkerTsConfig` option and reads it from the build target.
120+
if (testTarget && testTarget.builder !== Builders.BuildUnitTest) {
119121
(testTarget.options ??= {}).webWorkerTsConfig ??= workerConfigPath;
120122
}
121123
}),

‎packages/schematics/angular/web-worker/index_spec.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
1010
import { parse as parseJson } from 'jsonc-parser';
1111
import { Schema as ApplicationOptions } from '../application/schema';
12+
import { Builders } from '../utility/workspace-models';
1213
import { Schema as WorkspaceOptions } from '../workspace/schema';
1314
import { Schema as WebWorkerOptions } from './schema';
1415

@@ -68,6 +69,25 @@ describe('Web Worker Schematic', () => {
6869
);
6970
});
7071

72+
it('should not add the webWorkerTsConfig option to a unit-test builder test target', async () => {
73+
const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree);
74+
const { projects } = JSON.parse(tree.readContent('/angular.json'));
75+
expect(projects.bar.architect.test.builder).toBe(Builders.BuildUnitTest);
76+
expect(projects.bar.architect.test.options?.webWorkerTsConfig).toBeUndefined();
77+
});
78+
79+
it('should add the webWorkerTsConfig option to a karma builder test target', async () => {
80+
const config = JSON.parse(appTree.readContent('/angular.json'));
81+
config.projects.bar.architect.test = { builder: Builders.BuildKarma, options: {} };
82+
appTree.overwrite('/angular.json', JSON.stringify(config, undefined, 2));
83+
84+
const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree);
85+
const { projects } = JSON.parse(tree.readContent('/angular.json'));
86+
expect(projects.bar.architect.test.options.webWorkerTsConfig).toBe(
87+
'projects/bar/tsconfig.worker.json',
88+
);
89+
});
90+
7191
it('should add snippet to sibling file', async () => {
7292
const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree);
7393
const appComponent = tree.readContent('/projects/bar/src/app/app.ts');

0 commit comments

Comments
 (0)