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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
import { existsSync, promises } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { mergeProcessCovs } from '@bcoe/v8-coverage';
import astV8ToIstanbul from 'ast-v8-to-istanbul';
import libCoverage from 'istanbul-lib-coverage';
import libReport from 'istanbul-lib-report';
import reports from 'istanbul-reports';
import { parseModule } from 'magicast';
import { createDebug } from 'obug';
import { provider } from 'std-env';
import c from 'tinyrainbow';
import { BaseCoverageProvider } from 'vitest/coverage';
import { parseAstAsync } from 'vitest/node';
import { n as normalize } from './pathe.M-eThtNZ-BTaAGrLg.js';
var version = "4.0.18";
const FILE_PROTOCOL = "file://";
const debug = createDebug("vitest:coverage");
class V8CoverageProvider extends BaseCoverageProvider {
name = "v8";
version = version;
initialize(ctx) {
this._initialize(ctx);
}
createCoverageMap() {
return libCoverage.createCoverageMap({});
}
async generateCoverage({ allTestsRun }) {
const start = debug.enabled ? performance.now() : 0;
const coverageMap = this.createCoverageMap();
let merged = { result: [] };
await this.readCoverageFiles({
onFileRead(coverage) {
merged = mergeProcessCovs([merged, coverage]);
// mergeProcessCovs sometimes loses startOffset, e.g. in vue
merged.result.forEach((result) => {
if (!result.startOffset) {
const original = coverage.result.find((r) => r.url === result.url);
result.startOffset = original?.startOffset || 0;
}
});
},
onFinished: async (project, environment) => {
// Source maps can change based on projectName and transform mode.
// Coverage transform re-uses source maps so we need to separate transforms from each other.
const converted = await this.convertCoverage(merged, project, environment);
coverageMap.merge(converted);
merged = { result: [] };
},
onDebug: debug
});
// Include untested files when all tests were run (not a single file re-run)
// or if previous results are preserved by "cleanOnRerun: false"
if (this.options.include != null && (allTestsRun || !this.options.cleanOnRerun)) {
const coveredFiles = coverageMap.files();
const untestedCoverage = await this.getCoverageMapForUncoveredFiles(coveredFiles);
coverageMap.merge(untestedCoverage);
}
coverageMap.filter((filename) => {
const exists = existsSync(filename);
if (this.options.excludeAfterRemap) {
return exists && this.isIncluded(filename);
}
return exists;
});
if (debug.enabled) {
debug(`Generate coverage total time ${(performance.now() - start).toFixed()} ms`);
}
return coverageMap;
}
async generateReports(coverageMap, allTestsRun) {
if (provider === "stackblitz") {
this.ctx.logger.log(c.blue(" % ") + c.yellow("@vitest/coverage-v8 does not work on Stackblitz. Report will be empty."));
}
const context = libReport.createContext({
dir: this.options.reportsDirectory,
coverageMap,
watermarks: this.options.watermarks
});
if (this.hasTerminalReporter(this.options.reporter)) {
this.ctx.logger.log(c.blue(" % ") + c.dim("Coverage report from ") + c.yellow(this.name));
}
for (const reporter of this.options.reporter) {
// Type assertion required for custom reporters
reports.create(reporter[0], {
skipFull: this.options.skipFull,
projectRoot: this.ctx.config.root,
...reporter[1]
}).execute(context);
}
if (this.options.thresholds) {
await this.reportThresholds(coverageMap, allTestsRun);
}
}
async parseConfigModule(configFilePath) {
return parseModule(await promises.readFile(configFilePath, "utf8"));
}
async getCoverageMapForUncoveredFiles(testedFiles) {
const transform = this.createUncoveredFileTransformer(this.ctx);
const uncoveredFiles = await this.getUntestedFiles(testedFiles);
let index = 0;
const coverageMap = this.createCoverageMap();
for (const chunk of this.toSlices(uncoveredFiles, this.options.processingConcurrency)) {
if (debug.enabled) {
index += chunk.length;
debug("Uncovered files %d/%d", index, uncoveredFiles.length);
}
await Promise.all(chunk.map(async (filename) => {
let timeout;
let start;
if (debug.enabled) {
start = performance.now();
timeout = setTimeout(() => debug(c.bgRed(`File "${filename}" is taking longer than 3s`)), 3e3);
}
// Do not use pathToFileURL to avoid encoding filename parts
const url = `file://${filename[0] === "/" ? "" : "/"}${filename}`;
const sources = await this.getSources(url, transform);
coverageMap.merge(await this.remapCoverage(url, 0, sources, []));
if (debug.enabled) {
clearTimeout(timeout);
const diff = performance.now() - start;
const color = diff > 500 ? c.bgRed : c.bgGreen;
debug(`${color(` ${diff.toFixed()} ms `)} ${filename}`);
}
}));
}
return coverageMap;
}
async remapCoverage(filename, wrapperLength, result, functions) {
let ast;
try {
ast = await parseAstAsync(result.code);
} catch (error) {
this.ctx.logger.error(`Failed to parse ${filename}. Excluding it from coverage.\n`, error);
return {};
}
return await astV8ToIstanbul({
code: result.code,
sourceMap: result.map,
ast,
coverage: {
functions,
url: filename
},
ignoreClassMethods: this.options.ignoreClassMethods,
wrapperLength,
ignoreNode: (node, type) => {
// SSR transformed imports
if (type === "statement" && node.type === "VariableDeclarator" && node.id.type === "Identifier" && node.id.name.startsWith("__vite_ssr_import_")) {
return true;
}
// SSR transformed exports vite@>6.3.5
if (type === "statement" && node.type === "ExpressionStatement" && node.expression.type === "AssignmentExpression" && node.expression.left.type === "MemberExpression" && node.expression.left.object.type === "Identifier" && node.expression.left.object.name === "__vite_ssr_exports__") {
return true;
}
// SSR transformed exports vite@^6.3.5
if (type === "statement" && node.type === "VariableDeclarator" && node.id.type === "Identifier" && node.id.name === "__vite_ssr_export_default__") {
return true;
}
// CJS imports as ternaries - e.g.
// const React = __vite__cjsImport0_react.__esModule ? __vite__cjsImport0_react.default : __vite__cjsImport0_react;
if (type === "branch" && node.type === "ConditionalExpression" && node.test.type === "MemberExpression" && node.test.object.type === "Identifier" && node.test.object.name.startsWith("__vite__cjsImport") && node.test.property.type === "Identifier" && node.test.property.name === "__esModule") {
return true;
}
// in-source test with "if (import.meta.vitest)"
if ((type === "branch" || type === "statement") && node.type === "IfStatement" && node.test.type === "MemberExpression" && node.test.property.type === "Identifier" && node.test.property.name === "vitest") {
// SSR
if (node.test.object.type === "Identifier" && node.test.object.name === "__vite_ssr_import_meta__") {
return "ignore-this-and-nested-nodes";
}
// Web
if (node.test.object.type === "MetaProperty" && node.test.object.meta.name === "import" && node.test.object.property.name === "meta") {
return "ignore-this-and-nested-nodes";
}
}
// Browser mode's "import.meta.env ="
if (type === "statement" && node.type === "ExpressionStatement" && node.expression.type === "AssignmentExpression" && node.expression.left.type === "MemberExpression" && node.expression.left.object.type === "MetaProperty" && node.expression.left.object.meta.name === "import" && node.expression.left.object.property.name === "meta" && node.expression.left.property.type === "Identifier" && node.expression.left.property.name === "env") {
return true;
}
// SSR mode's "import.meta.env ="
if (type === "statement" && node.type === "ExpressionStatement" && node.expression.type === "AssignmentExpression" && node.expression.left.type === "MemberExpression" && node.expression.left.object.type === "Identifier" && node.expression.left.object.name === "__vite_ssr_import_meta__") {
return true;
}
// SWC's decorators
if (type === "statement" && node.type === "ExpressionStatement" && node.expression.type === "CallExpression" && node.expression.callee.type === "Identifier" && node.expression.callee.name === "_ts_decorate") {
return "ignore-this-and-nested-nodes";
}
}
});
}
async getSources(url, onTransform, functions = []) {
const transformResult = await onTransform(removeStartsWith(url, FILE_PROTOCOL)).catch(() => undefined);
const map = transformResult?.map;
const code = transformResult?.code;
if (code == null) {
const filePath = normalize(fileURLToPath(url));
const original = await promises.readFile(filePath, "utf-8").catch(() => {
// If file does not exist construct a dummy source for it.
// These can be files that were generated dynamically during the test run and were removed after it.
const length = findLongestFunctionLength(functions);
return "/".repeat(length);
});
return { code: original };
}
// Vue needs special handling for "map.sources"
if (map) {
map.sources ||= [];
map.sources = map.sources.filter((source) => source != null).map((source) => new URL(source, url).href);
if (map.sources.length === 0) {
map.sources.push(url);
}
}
return {
code,
map
};
}
async convertCoverage(coverage, project = this.ctx.getRootProject(), environment) {
if (environment === "__browser__" && !project.browser) {
throw new Error(`Cannot access browser module graph because it was torn down.`);
}
async function onTransform(filepath) {
if (environment === "__browser__" && project.browser) {
const result = await project.browser.vite.transformRequest(removeStartsWith(filepath, project.config.root));
if (result) {
return {
...result,
code: `${result.code}// <inline-source-map>`
};
}
}
return project.vite.environments[environment].transformRequest(filepath);
}
const scriptCoverages = [];
for (const result of coverage.result) {
if (environment === "__browser__") {
if (result.url.startsWith("/@fs")) {
result.url = `${FILE_PROTOCOL}${removeStartsWith(result.url, "/@fs")}`;
} else if (result.url.startsWith(project.config.root)) {
result.url = `${FILE_PROTOCOL}${result.url}`;
} else {
result.url = `${FILE_PROTOCOL}${project.config.root}${result.url}`;
}
}
if (this.isIncluded(fileURLToPath(result.url))) {
scriptCoverages.push({
...result,
url: decodeURIComponent(result.url)
});
}
}
const coverageMap = this.createCoverageMap();
let index = 0;
for (const chunk of this.toSlices(scriptCoverages, this.options.processingConcurrency)) {
if (debug.enabled) {
index += chunk.length;
debug("Converting %d/%d", index, scriptCoverages.length);
}
await Promise.all(chunk.map(async ({ url, functions, startOffset }) => {
let timeout;
let start;
if (debug.enabled) {
start = performance.now();
timeout = setTimeout(() => debug(c.bgRed(`File "${fileURLToPath(url)}" is taking longer than 3s`)), 3e3);
}
const sources = await this.getSources(url, onTransform, functions);
coverageMap.merge(await this.remapCoverage(url, startOffset, sources, functions));
if (debug.enabled) {
clearTimeout(timeout);
const diff = performance.now() - start;
const color = diff > 500 ? c.bgRed : c.bgGreen;
debug(`${color(` ${diff.toFixed()} ms `)} ${fileURLToPath(url)}`);
}
}));
}
return coverageMap;
}
}
/**
* Find the function with highest `endOffset` to determine the length of the file
*/
function findLongestFunctionLength(functions) {
return functions.reduce((previous, current) => {
const maxEndOffset = current.ranges.reduce((endOffset, range) => Math.max(endOffset, range.endOffset), 0);
return Math.max(previous, maxEndOffset);
}, 0);
}
function removeStartsWith(filepath, start) {
if (filepath.startsWith(start)) {
return filepath.slice(start.length);
}
return filepath;
}
export { V8CoverageProvider };
|