Skip to content

fix(@schematics/angular): remove karma config devkit package usages during application migration #30415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ function updateProjects(tree: Tree, context: SchematicContext) {

// Use @angular/build directly if there is no devkit package usage
if (!hasAngularDevkitUsage) {
const karmaConfigFiles = new Set<string>();

for (const [, target] of allWorkspaceTargets(workspace)) {
switch (target.builder) {
case Builders.Application:
Expand All @@ -245,9 +247,15 @@ function updateProjects(tree: Tree, context: SchematicContext) {
break;
case Builders.Karma:
target.builder = '@angular/build:karma';
// Remove "builderMode" option since the builder will always use "application"
for (const [, karmaOptions] of allTargetOptions(target)) {
// Remove "builderMode" option since the builder will always use "application"
delete karmaOptions['builderMode'];

// Collect custom karma configurations for @angular-devkit/build-angular plugin removal
const karmaConfig = karmaOptions['karmaConfig'];
if (karmaConfig && typeof karmaConfig === 'string') {
karmaConfigFiles.add(karmaConfig);
}
}
break;
case Builders.NgPackagr:
Expand Down Expand Up @@ -292,6 +300,34 @@ function updateProjects(tree: Tree, context: SchematicContext) {
}),
);
}

for (const karmaConfigFile of karmaConfigFiles) {
if (!tree.exists(karmaConfigFile)) {
continue;
}

try {
const originalKarmaConfigText = tree.readText(karmaConfigFile);
const updatedKarmaConfigText = originalKarmaConfigText
.replaceAll(`require('@angular-devkit/build-angular/plugins/karma'),`, '')
.replaceAll(`require('@angular-devkit/build-angular/plugins/karma')`, '');

if (updatedKarmaConfigText.includes('@angular-devkit/build-angular/plugins')) {
throw new Error(
'Migration does not support found usage of "@angular-devkit/build-angular".',
);
} else {
tree.overwrite(karmaConfigFile, updatedKarmaConfigText);
}
} catch (error) {
const reason = error instanceof Error ? `Reason: ${error.message}` : '';
context.logger.warn(
`Unable to update custom karma configuration file ("${karmaConfigFile}"). ` +
reason +
'\nReferences to the "@angular-devkit/build-angular" package within the file may need to be removed manually.',
);
}
}
}

return chain(rules);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,103 @@ describe(`Migration to use the application builder`, () => {
expect(builderMode).toBeUndefined();
});

it(`should update file for 'karmaConfig' karma option (no require trailing comma)`, async () => {
addWorkspaceTarget(tree, 'test', {
'builder': Builders.Karma,
'options': {
'karmaConfig': './karma.conf.js',
'polyfills': ['zone.js', 'zone.js/testing'],
'tsConfig': 'projects/app-a/tsconfig.spec.json',
},
});
tree.create(
'./karma.conf.js',
`
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage'),
require('@angular-devkit/build-angular/plugins/karma')
]
});
};`,
);

const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const {
projects: { app },
} = JSON.parse(newTree.readContent('/angular.json'));

const { karmaConfig } = app.architect['test'].options;
expect(karmaConfig).toBe('./karma.conf.js');

const karmaConfigText = newTree.readText('./karma.conf.js');
expect(karmaConfigText).not.toContain(`require('@angular-devkit/build-angular/plugins/karma')`);
});

it(`should update file for 'karmaConfig' karma option (require trailing comma)`, async () => {
addWorkspaceTarget(tree, 'test', {
'builder': Builders.Karma,
'options': {
'karmaConfig': './karma.conf.js',
'polyfills': ['zone.js', 'zone.js/testing'],
'tsConfig': 'projects/app-a/tsconfig.spec.json',
},
});
tree.create(
'./karma.conf.js',
`
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage'),
require('@angular-devkit/build-angular/plugins/karma'),
]
});
};`,
);

const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const {
projects: { app },
} = JSON.parse(newTree.readContent('/angular.json'));

const { karmaConfig } = app.architect['test'].options;
expect(karmaConfig).toBe('./karma.conf.js');

const karmaConfigText = newTree.readText('./karma.conf.js');
expect(karmaConfigText).not.toContain(`require('@angular-devkit/build-angular/plugins/karma')`);
});

it(`should ignore missing file for 'karmaConfig' karma option`, async () => {
addWorkspaceTarget(tree, 'test', {
'builder': Builders.Karma,
'options': {
'karmaConfig': './karma.conf.js',
'polyfills': ['zone.js', 'zone.js/testing'],
'tsConfig': 'projects/app-a/tsconfig.spec.json',
},
});

const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const {
projects: { app },
} = JSON.parse(newTree.readContent('/angular.json'));

const { karmaConfig } = app.architect['test'].options;
expect(karmaConfig).toBe('./karma.conf.js');
});

it('should remove tilde prefix from CSS @import specifiers', async () => {
// Replace outputPath
tree.create(
Expand Down