Skip to content

Commit 2ae38c8

Browse files
committed
fix(@schematics/angular): keep base test build options in per-configuration Karma migration
The Karma to Vitest migration moves build-only test options such as `styles` into a `testing` build configuration and points the test target at it. For each test configuration it created a separate `testing-<name>` build configuration containing only that configuration's own options. Because a test configuration's `buildTarget` replaces the base one, running `ng test --configuration <name>` lost the options moved from the base test options. Merge the base options into each per-configuration build configuration, with the configuration's own values taking precedence.
1 parent 0fd4404 commit 2ae38c8

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎packages/schematics/angular/migrations/migrate-karma-to-vitest/migration.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,15 @@ function updateProjects(tree: Tree, context: SchematicContext): Rule {
306306
for (const [configKey, configOptions] of Object.entries(customBuildOptions)) {
307307
const finalConfig: Record<string, json.JsonValue | undefined> = {};
308308

309+
// A test configuration inherits the base test options, so its build configuration must
310+
// include the build options moved from them, with the configuration's own values on top.
311+
const mergedOptions = configKey
312+
? { ...customBuildOptions[''], ...configOptions }
313+
: configOptions;
314+
309315
// Omit options that already have the same value in the base build options.
310316
// Using isDeepStrictEqual for a deep comparison of arrays and objects.
311-
for (const [key, value] of Object.entries(configOptions)) {
317+
for (const [key, value] of Object.entries(mergedOptions)) {
312318
if (!isDeepStrictEqual(value, baseOptions[key])) {
313319
finalConfig[key] = value;
314320
}

‎packages/schematics/angular/migrations/migrate-karma-to-vitest/migration_spec.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,28 @@ describe('Migration from Karma to Vitest unit-test builder', () => {
128128
expect(newProjects.app.targets.test.configurations.ci.assets).toBeUndefined();
129129
});
130130

131+
it('should keep base custom build options in per configuration testing configurations', async () => {
132+
const { projects } = tree.readJson('/angular.json') as any;
133+
projects.app.targets.test.options.styles = ['src/test-styles.scss'];
134+
projects.app.targets.test.configurations = {
135+
ci: {
136+
watch: false,
137+
},
138+
};
139+
tree.overwrite('/angular.json', JSON.stringify({ version: 1, projects }));
140+
141+
const newTree = await schematicRunner.runSchematic('migrate-karma-to-vitest', {}, tree);
142+
const { projects: newProjects } = newTree.readJson('/angular.json') as any;
143+
144+
expect(newProjects.app.targets.build.configurations.testing.styles).toEqual([
145+
'src/test-styles.scss',
146+
]);
147+
expect(newProjects.app.targets.test.configurations.ci.buildTarget).toBe(':build:testing-ci');
148+
expect(newProjects.app.targets.build.configurations['testing-ci'].styles).toEqual([
149+
'src/test-styles.scss',
150+
]);
151+
});
152+
131153
it('should omit testing configuration if options match base build options', async () => {
132154
const { projects } = tree.readJson('/angular.json') as any;
133155
projects.app.targets.build.options.aot = false;

0 commit comments

Comments
 (0)