@vitest/coverage-v8 0.33.0 → 0.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/provider.js +44 -9
  2. package/package.json +4 -4
package/dist/provider.js CHANGED
@@ -10,6 +10,7 @@ import MagicString from 'magic-string';
10
10
  import remapping from '@ampproject/remapping';
11
11
  import c from 'picocolors';
12
12
  import { provider } from 'std-env';
13
+ import { builtinModules } from 'module';
13
14
  import { coverageConfigDefaults, defaultExclude, defaultInclude } from 'vitest/config';
14
15
  import { BaseCoverageProvider } from 'vitest/coverage';
15
16
  import _TestExclude from 'test-exclude';
@@ -140,6 +141,31 @@ const isAbsolute = function(p) {
140
141
  return _IS_ABSOLUTE_RE.test(p);
141
142
  };
142
143
 
144
+ const isWindows = process.platform === "win32";
145
+ const drive = isWindows ? process.cwd()[0] : null;
146
+ drive ? drive === drive.toUpperCase() ? drive.toLowerCase() : drive.toUpperCase() : null;
147
+ const queryRE = /\?.*$/s;
148
+ const hashRE = /#.*$/s;
149
+ function cleanUrl(url) {
150
+ return url.replace(hashRE, "").replace(queryRE, "");
151
+ }
152
+ /* @__PURE__ */ new Set([
153
+ ...builtinModules,
154
+ "assert/strict",
155
+ "diagnostics_channel",
156
+ "dns/promises",
157
+ "fs/promises",
158
+ "path/posix",
159
+ "path/win32",
160
+ "readline/promises",
161
+ "stream/consumers",
162
+ "stream/promises",
163
+ "stream/web",
164
+ "timers/promises",
165
+ "util/types",
166
+ "wasi"
167
+ ]);
168
+
143
169
  const WRAPPER_LENGTH = 185;
144
170
  const VITE_EXPORTS_LINE_PATTERN = /Object\.defineProperty\(__vite_ssr_exports__.*\n/g;
145
171
  class V8CoverageProvider extends BaseCoverageProvider {
@@ -186,15 +212,16 @@ class V8CoverageProvider extends BaseCoverageProvider {
186
212
  async reportCoverage({ allTestsRun } = {}) {
187
213
  if (provider === "stackblitz")
188
214
  this.ctx.logger.log(c.blue(" % ") + c.yellow("@vitest/coverage-v8 does not work on Stackblitz. Report will be empty."));
215
+ const transformResults = normalizeTransformResults(this.ctx.projects.map((project) => project.vitenode.fetchCache));
189
216
  const merged = mergeProcessCovs(this.coverages);
190
217
  const scriptCoverages = merged.result.filter((result) => this.testExclude.shouldInstrument(fileURLToPath(result.url)));
191
218
  if (this.options.all && allTestsRun) {
192
219
  const coveredFiles = Array.from(scriptCoverages.map((r) => r.url));
193
- const untestedFiles = await this.getUntestedFiles(coveredFiles);
220
+ const untestedFiles = await this.getUntestedFiles(coveredFiles, transformResults);
194
221
  scriptCoverages.push(...untestedFiles);
195
222
  }
196
223
  const converted = await Promise.all(scriptCoverages.map(async ({ url, functions }) => {
197
- const sources = await this.getSources(url, functions);
224
+ const sources = await this.getSources(url, transformResults, functions);
198
225
  const wrapperLength = sources.sourceMap ? WRAPPER_LENGTH : 0;
199
226
  const converter = v8ToIstanbul(url, wrapperLength, sources);
200
227
  await converter.load();
@@ -247,11 +274,11 @@ class V8CoverageProvider extends BaseCoverageProvider {
247
274
  });
248
275
  }
249
276
  }
250
- async getUntestedFiles(testedFiles) {
277
+ async getUntestedFiles(testedFiles, transformResults) {
251
278
  const includedFiles = await this.testExclude.glob(this.ctx.config.root);
252
279
  const uncoveredFiles = includedFiles.map((file) => pathToFileURL(resolve(this.ctx.config.root, file))).filter((file) => !testedFiles.includes(file.href));
253
280
  return await Promise.all(uncoveredFiles.map(async (uncoveredFile) => {
254
- const { source } = await this.getSources(uncoveredFile.href);
281
+ const { source } = await this.getSources(uncoveredFile.href, transformResults);
255
282
  return {
256
283
  url: uncoveredFile.href,
257
284
  scriptId: "0",
@@ -269,13 +296,10 @@ class V8CoverageProvider extends BaseCoverageProvider {
269
296
  };
270
297
  }));
271
298
  }
272
- async getSources(url, functions = []) {
299
+ async getSources(url, transformResults, functions = []) {
273
300
  var _a;
274
301
  const filePath = normalize(fileURLToPath(url));
275
- const transformResult = this.ctx.projects.map((project) => {
276
- var _a2;
277
- return (_a2 = project.vitenode.fetchCache.get(filePath)) == null ? void 0 : _a2.result;
278
- }).filter(Boolean).shift();
302
+ const transformResult = transformResults.get(filePath);
279
303
  const map = transformResult == null ? void 0 : transformResult.map;
280
304
  const code = transformResult == null ? void 0 : transformResult.code;
281
305
  const sourcesContent = ((_a = map == null ? void 0 : map.sourcesContent) == null ? void 0 : _a[0]) || await promises.readFile(filePath, "utf-8").catch(() => {
@@ -318,5 +342,16 @@ function findLongestFunctionLength(functions) {
318
342
  return Math.max(previous, maxEndOffset);
319
343
  }, 0);
320
344
  }
345
+ function normalizeTransformResults(fetchCaches) {
346
+ const normalized = /* @__PURE__ */ new Map();
347
+ for (const fetchCache of fetchCaches) {
348
+ for (const [key, value] of fetchCache.entries()) {
349
+ const cleanEntry = cleanUrl(key);
350
+ if (!normalized.has(cleanEntry))
351
+ normalized.set(cleanEntry, value.result);
352
+ }
353
+ }
354
+ return normalized;
355
+ }
321
356
 
322
357
  export { V8CoverageProvider };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/coverage-v8",
3
3
  "type": "module",
4
- "version": "0.33.0",
4
+ "version": "0.34.0",
5
5
  "description": "V8 coverage provider for Vitest",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -43,7 +43,7 @@
43
43
  "@ampproject/remapping": "^2.2.1",
44
44
  "@bcoe/v8-coverage": "^0.2.3",
45
45
  "istanbul-lib-coverage": "^3.2.0",
46
- "istanbul-lib-report": "^3.0.0",
46
+ "istanbul-lib-report": "^3.0.1",
47
47
  "istanbul-lib-source-maps": "^4.0.1",
48
48
  "istanbul-reports": "^3.1.5",
49
49
  "magic-string": "^0.30.1",
@@ -58,8 +58,8 @@
58
58
  "@types/istanbul-lib-source-maps": "^4.0.1",
59
59
  "@types/istanbul-reports": "^3.0.1",
60
60
  "pathe": "^1.1.1",
61
- "vitest": "0.33.0",
62
- "vite-node": "0.33.0"
61
+ "vite-node": "0.34.0",
62
+ "vitest": "0.34.0"
63
63
  },
64
64
  "scripts": {
65
65
  "build": "rimraf dist && rollup -c",