|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import type { json } from '@angular-devkit/core'; |
| 10 | +import { SchematicContext, Tree } from '@angular-devkit/schematics'; |
| 11 | +import { KarmaConfigAnalysis, analyzeKarmaConfig } from '../karma/karma-config-analyzer'; |
| 12 | +import { compareKarmaConfigToDefault, hasDifferences } from '../karma/karma-config-comparer'; |
| 13 | +import { SUPPORTED_COVERAGE_REPORTERS, SUPPORTED_REPORTERS } from './constants'; |
| 14 | + |
| 15 | +function extractReporters( |
| 16 | + analysis: KarmaConfigAnalysis, |
| 17 | + options: Record<string, json.JsonValue | undefined>, |
| 18 | + projectName: string, |
| 19 | + context: SchematicContext, |
| 20 | +): void { |
| 21 | + const reporters = analysis.settings.get('reporters'); |
| 22 | + if (Array.isArray(reporters)) { |
| 23 | + const mappedReporters: string[] = []; |
| 24 | + for (const r of reporters) { |
| 25 | + if (typeof r === 'string') { |
| 26 | + if (r === 'progress') { |
| 27 | + mappedReporters.push('default'); |
| 28 | + } else if (r === 'kjhtml') { |
| 29 | + context.logger.warn( |
| 30 | + `Project "${projectName}" uses the "kjhtml" reporter. ` + |
| 31 | + `This has not been automatically mapped. ` + |
| 32 | + `For an interactive test UI in Vitest, consider setting the "ui" option to true in your test target options ` + |
| 33 | + `and installing "@vitest/ui".`, |
| 34 | + ); |
| 35 | + } else if (SUPPORTED_REPORTERS.has(r)) { |
| 36 | + mappedReporters.push(r); |
| 37 | + } else { |
| 38 | + context.logger.warn( |
| 39 | + `Project "${projectName}" uses a custom Karma reporter "${r}". ` + |
| 40 | + `This reporter cannot be automatically mapped to Vitest. ` + |
| 41 | + `Please check the Vitest documentation for equivalent reporters.`, |
| 42 | + ); |
| 43 | + } |
| 44 | + } else { |
| 45 | + context.logger.warn( |
| 46 | + `Project "${projectName}" has a non-string reporter in Karma config. ` + |
| 47 | + `This cannot be automatically mapped to Vitest.`, |
| 48 | + ); |
| 49 | + } |
| 50 | + } |
| 51 | + if (mappedReporters.length > 0) { |
| 52 | + options['reporters'] = [...new Set(mappedReporters)]; |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function extractCoverageSettings( |
| 58 | + analysis: KarmaConfigAnalysis, |
| 59 | + options: Record<string, json.JsonValue | undefined>, |
| 60 | + projectName: string, |
| 61 | + context: SchematicContext, |
| 62 | +): void { |
| 63 | + const coverageReporter = analysis.settings.get('coverageReporter'); |
| 64 | + if (typeof coverageReporter !== 'object' || coverageReporter === null) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // Extract coverage reporters |
| 69 | + const covReporters = (coverageReporter as Record<string, unknown>)['reporters']; |
| 70 | + if (Array.isArray(covReporters)) { |
| 71 | + const mappedCovReporters: string[] = []; |
| 72 | + for (const r of covReporters) { |
| 73 | + let type: string | undefined; |
| 74 | + if (typeof r === 'object' && r !== null && 'type' in r) { |
| 75 | + if (typeof r['type'] === 'string') { |
| 76 | + type = r['type']; |
| 77 | + } |
| 78 | + } else if (typeof r === 'string') { |
| 79 | + type = r; |
| 80 | + } |
| 81 | + |
| 82 | + if (type) { |
| 83 | + if (SUPPORTED_COVERAGE_REPORTERS.has(type)) { |
| 84 | + mappedCovReporters.push(type); |
| 85 | + } else { |
| 86 | + context.logger.warn( |
| 87 | + `Project "${projectName}" uses a custom coverage reporter "${type}". ` + |
| 88 | + `This reporter cannot be automatically mapped to Vitest. ` + |
| 89 | + `Please check the Vitest documentation for equivalent coverage reporters.`, |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + if (mappedCovReporters.length > 0) { |
| 95 | + options['coverageReporters'] = [...new Set(mappedCovReporters)]; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Extract coverage thresholds |
| 100 | + const check = (coverageReporter as Record<string, unknown>)['check']; |
| 101 | + if (typeof check === 'object' && check !== null) { |
| 102 | + const global = (check as Record<string, unknown>)['global']; |
| 103 | + if (typeof global === 'object' && global !== null) { |
| 104 | + const thresholds: Record<string, number> = {}; |
| 105 | + const keys = ['statements', 'branches', 'functions', 'lines']; |
| 106 | + for (const key of keys) { |
| 107 | + const value = (global as Record<string, unknown>)[key]; |
| 108 | + if (typeof value === 'number') { |
| 109 | + thresholds[key] = value; |
| 110 | + } |
| 111 | + } |
| 112 | + if (Object.keys(thresholds).length > 0) { |
| 113 | + options['coverageThresholds'] = { |
| 114 | + ...thresholds, |
| 115 | + perFile: false, |
| 116 | + }; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +export async function processKarmaConfig( |
| 123 | + karmaConfig: string, |
| 124 | + options: Record<string, json.JsonValue | undefined>, |
| 125 | + projectName: string, |
| 126 | + context: SchematicContext, |
| 127 | + tree: Tree, |
| 128 | + removableKarmaConfigs: Map<string, boolean>, |
| 129 | + needDevkitPlugin: boolean, |
| 130 | + manualMigrationFiles: string[], |
| 131 | +): Promise<void> { |
| 132 | + if (tree.exists(karmaConfig)) { |
| 133 | + const content = tree.readText(karmaConfig); |
| 134 | + const analysis = analyzeKarmaConfig(content); |
| 135 | + |
| 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); |
| 153 | + } |
| 154 | + |
| 155 | + if (isRemovable) { |
| 156 | + tree.delete(karmaConfig); |
| 157 | + } else { |
| 158 | + context.logger.warn( |
| 159 | + `Project "${projectName}" uses a custom Karma configuration file "${karmaConfig}". ` + |
| 160 | + `Tests have been migrated to use Vitest, but you may need to manually migrate custom settings ` + |
| 161 | + `from this Karma config to a Vitest config (e.g. vitest.config.ts).`, |
| 162 | + ); |
| 163 | + manualMigrationFiles.push(karmaConfig); |
| 164 | + } |
| 165 | + } |
| 166 | + delete options['karmaConfig']; |
| 167 | +} |
0 commit comments