| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent edfa782 commit aed407d
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -119,42 +119,47 @@ function extractCoverageSettings( | |||
| 119 | 119 | } | |
| 120 | 120 | } | |
| 121 | 121 | ||
| 122 | + export interface KarmaConfigProcessingResult { | ||
| 123 | + analysis: KarmaConfigAnalysis; | ||
| 124 | + isRemovable: boolean; | ||
| 125 | + } | ||
| 126 | + | ||
| 122 | 127 | export async function processKarmaConfig( | |
| 123 | 128 | karmaConfig: string, | |
| 124 | 129 | options: Record<string, json.JsonValue | undefined>, | |
| 125 | 130 | projectName: string, | |
| 126 | 131 | context: SchematicContext, | |
| 127 | 132 | tree: Tree, | |
| 128 | - removableKarmaConfigs: Map<string, boolean>, | ||
| 133 | + cache: Map<string, KarmaConfigProcessingResult>, | ||
| 129 | 134 | needDevkitPlugin: boolean, | |
| 130 | 135 | manualMigrationFiles: string[], | |
| 131 | 136 | ): Promise<void> { | |
| 132 | - if (tree.exists(karmaConfig)) { | ||
| 137 | + let cachedResult = cache.get(karmaConfig); | ||
| 138 | + | ||
| 139 | + if (!cachedResult && tree.exists(karmaConfig)) { | ||
| 133 | 140 | const content = tree.readText(karmaConfig); | |
| 134 | 141 | const analysis = analyzeKarmaConfig(content); | |
| 135 | 142 | ||
| 136 | - extractReporters(analysis, options, projectName, context); | ||
| 137 | - extractCoverageSettings(analysis, options, projectName, context); | ||
| 138 | - | ||
| 139 | - let isRemovable = removableKarmaConfigs.get(karmaConfig); | ||
| 140 | - if (isRemovable === undefined) { | ||
| 141 | - if (analysis.hasUnsupportedValues) { | ||
| 142 | - isRemovable = false; | ||
| 143 | - } else { | ||
| 144 | - const diff = await compareKarmaConfigToDefault( | ||
| 145 | - analysis, | ||
| 146 | - projectName, | ||
| 147 | - karmaConfig, | ||
| 148 | - needDevkitPlugin, | ||
| 149 | - ); | ||
| 150 | - isRemovable = !hasDifferences(diff) && diff.isReliable; | ||
| 151 | - } | ||
| 152 | - removableKarmaConfigs.set(karmaConfig, isRemovable); | ||
| 143 | + let isRemovable = false; | ||
| 144 | + if (!analysis.hasUnsupportedValues) { | ||
| 145 | + const diff = await compareKarmaConfigToDefault( | ||
| 146 | + analysis, | ||
| 147 | + projectName, | ||
| 148 | + karmaConfig, | ||
| 149 | + needDevkitPlugin, | ||
| 150 | + ); | ||
| 151 | + isRemovable = !hasDifferences(diff) && diff.isReliable; | ||
| 153 | 152 | } | |
| 154 | 153 | ||
| 155 | - if (isRemovable) { | ||
| 156 | - tree.delete(karmaConfig); | ||
| 157 | - } else { | ||
| 154 | + cachedResult = { analysis, isRemovable }; | ||
| 155 | + cache.set(karmaConfig, cachedResult); | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + if (cachedResult) { | ||
| 159 | + extractReporters(cachedResult.analysis, options, projectName, context); | ||
| 160 | + extractCoverageSettings(cachedResult.analysis, options, projectName, context); | ||
| 161 | + | ||
| 162 | + if (!cachedResult.isRemovable) { | ||
| 158 | 163 | context.logger.warn( | |
| 159 | 164 | `Project "${projectName}" uses a custom Karma configuration file "${karmaConfig}". ` + | |
| 160 | 165 | `Tests have been migrated to use Vitest, but you may need to manually migrate custom settings ` + | |
@@ -164,5 +169,6 @@ export async function processKarmaConfig( | |||
| 164 | 169 | manualMigrationFiles.push(karmaConfig); | |
| 165 | 170 | } | |
| 166 | 171 | } | |
| 172 | + | ||
| 167 | 173 | delete options['karmaConfig']; | |
| 168 | 174 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,14 +14,14 @@ import { latestVersions } from '../../utility/latest-versions'; | |||
| 14 | 14 | import { TargetDefinition, allTargetOptions, updateWorkspace } from '../../utility/workspace'; | |
| 15 | 15 | import { Builders } from '../../utility/workspace-models'; | |
| 16 | 16 | import { BUILD_OPTIONS_KEYS } from './constants'; | |
| 17 | - import { processKarmaConfig } from './karma-processor'; | ||
| 17 | + import { KarmaConfigProcessingResult, processKarmaConfig } from './karma-processor'; | ||
| 18 | 18 | ||
| 19 | 19 | async function processTestTargetOptions( | |
| 20 | 20 | testTarget: TargetDefinition, | |
| 21 | 21 | projectName: string, | |
| 22 | 22 | context: SchematicContext, | |
| 23 | 23 | tree: Tree, | |
| 24 | - removableKarmaConfigs: Map<string, boolean>, | ||
| 24 | + removableKarmaConfigs: Map<string, KarmaConfigProcessingResult>, | ||
| 25 | 25 | customBuildOptions: Record<string, Record<string, json.JsonValue | undefined>>, | |
| 26 | 26 | needDevkitPlugin: boolean, | |
| 27 | 27 | manualMigrationFiles: string[], | |
@@ -122,7 +122,7 @@ async function processTestTargetOptions( | |||
| 122 | 122 | function updateProjects(tree: Tree, context: SchematicContext): Rule { | |
| 123 | 123 | return updateWorkspace(async (workspace) => { | |
| 124 | 124 | let needsCoverage = false; | |
| 125 | - const removableKarmaConfigs = new Map<string, boolean>(); | ||
| 125 | + const removableKarmaConfigs = new Map<string, KarmaConfigProcessingResult>(); | ||
| 126 | 126 | const migratedProjects: string[] = []; | |
| 127 | 127 | const skippedNonApplications: string[] = []; | |
| 128 | 128 | const skippedMissingAppBuilder: string[] = []; | |
@@ -235,6 +235,13 @@ function updateProjects(tree: Tree, context: SchematicContext): Rule { | |||
| 235 | 235 | migratedProjects.push(projectName); | |
| 236 | 236 | } | |
| 237 | 237 | ||
| 238 | + // Perform cleanup of removable karma config files | ||
| 239 | + for (const [configPath, result] of removableKarmaConfigs) { | ||
| 240 | + if (result.isRemovable && tree.exists(configPath)) { | ||
| 241 | + tree.delete(configPath); | ||
| 242 | + } | ||
| 243 | + } | ||
| 244 | + | ||
| 238 | 245 | // Log summary | |
| 239 | 246 | context.logger.info('\n--- Karma to Vitest Migration Summary ---'); | |
| 240 | 247 | context.logger.info(`Projects migrated: ${migratedProjects.length}`); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -322,6 +322,7 @@ module.exports = function (config) { | |||
| 322 | 322 | const newTree = await schematicRunner.runSchematic('migrate-karma-to-vitest', {}, tree); | |
| 323 | 323 | expect(newTree.exists('karma.conf.js')).toBeFalse(); | |
| 324 | 324 | }); | |
| 325 | + | ||
| 325 | 326 | it('should shift main compilation entry file directly into setupFiles array', async () => { | |
| 326 | 327 | const { projects } = tree.readJson('/angular.json') as any; | |
| 327 | 328 | projects.app.targets.test.options.main = 'src/test.ts'; | |
@@ -333,6 +334,7 @@ module.exports = function (config) { | |||
| 333 | 334 | expect(newProjects.app.targets.test.options.setupFiles).toEqual(['src/test.ts']); | |
| 334 | 335 | expect(newProjects.app.targets.test.options.main).toBeUndefined(); | |
| 335 | 336 | }); | |
| 337 | + | ||
| 336 | 338 | it('should generate unique testing configuration name preventing collision overwrites', async () => { | |
| 337 | 339 | const { projects } = tree.readJson('/angular.json') as any; | |
| 338 | 340 | projects.app.targets.build.configurations = { | |
@@ -350,6 +352,7 @@ module.exports = function (config) { | |||
| 350 | 352 | ]); | |
| 351 | 353 | expect(newProjects.app.targets.test.options.buildTarget).toBe(':build:testing-2'); | |
| 352 | 354 | }); | |
| 355 | + | ||
| 353 | 356 | it('should inject @vitest/coverage-v8 whenever coverage presence is detected', async () => { | |
| 354 | 357 | const { projects } = tree.readJson('/angular.json') as any; | |
| 355 | 358 | projects.app.targets.test.options.codeCoverage = true; | |
@@ -360,4 +363,61 @@ module.exports = function (config) { | |||
| 360 | 363 | ||
| 361 | 364 | expect(devDependencies['@vitest/coverage-v8']).toBe(latestVersions['@vitest/coverage-v8']); | |
| 362 | 365 | }); | |
| 366 | + | ||
| 367 | + it('should successfully extract settings across multiple projects sharing the same removable karma config', async () => { | ||
| 368 | + const { projects } = tree.readJson('/angular.json') as any; | ||
| 369 | + projects.app.targets.test.builder = '@angular-devkit/build-angular:karma'; | ||
| 370 | + | ||
| 371 | + // Add a second project sharing the exact same configuration | ||
| 372 | + projects.app2 = { | ||
| 373 | + ...JSON.parse(JSON.stringify(projects.app)), | ||
| 374 | + root: 'app2', | ||
| 375 | + }; | ||
| 376 | + | ||
| 377 | + tree.overwrite('/angular.json', JSON.stringify({ version: 1, projects })); | ||
| 378 | + | ||
| 379 | + const DEFAULT_KARMA_CONFIG = ` | ||
| 380 | + module.exports = function (config) { | ||
| 381 | + config.set({ | ||
| 382 | + basePath: '', | ||
| 383 | + frameworks: ['jasmine', '@angular-devkit/build-angular'], | ||
| 384 | + plugins: [ | ||
| 385 | + require('karma-jasmine'), | ||
| 386 | + require('karma-chrome-launcher'), | ||
| 387 | + require('karma-jasmine-html-reporter'), | ||
| 388 | + require('karma-coverage'), | ||
| 389 | + require('@angular-devkit/build-angular/plugins/karma') | ||
| 390 | + ], | ||
| 391 | + client: { | ||
| 392 | + jasmine: {}, | ||
| 393 | + }, | ||
| 394 | + jasmineHtmlReporter: { | ||
| 395 | + suppressAll: true | ||
| 396 | + }, | ||
| 397 | + coverageReporter: { | ||
| 398 | + dir: require('path').join(__dirname, './coverage/app'), | ||
| 399 | + subdir: '.', | ||
| 400 | + reporters: [ | ||
| 401 | + { type: 'html' }, | ||
| 402 | + { type: 'text-summary' } | ||
| 403 | + ] | ||
| 404 | + }, | ||
| 405 | + reporters: ['progress', 'kjhtml'], | ||
| 406 | + browsers: ['Chrome'], | ||
| 407 | + restartOnFileChange: true | ||
| 408 | + }); | ||
| 409 | + }; | ||
| 410 | + `; | ||
| 411 | + tree.create('karma.conf.js', DEFAULT_KARMA_CONFIG); | ||
| 412 | + | ||
| 413 | + const newTree = await schematicRunner.runSchematic('migrate-karma-to-vitest', {}, tree); | ||
| 414 | + const { projects: newProjects } = newTree.readJson('/angular.json') as any; | ||
| 415 | + | ||
| 416 | + // Assert BOTH projects got the extraction logic mapped correctly | ||
| 417 | + expect(newProjects.app.targets.test.options.reporters).toEqual(['default']); | ||
| 418 | + expect(newProjects.app2.targets.test.options.reporters).toEqual(['default']); | ||
| 419 | + | ||
| 420 | + // Assert that the deletion deferred successfully until BOTH extracted the data | ||
| 421 | + expect(newTree.exists('karma.conf.js')).toBeFalse(); | ||
| 422 | + }); | ||
| 363 | 423 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments