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