@vitest/coverage-istanbul 5.0.3 → 5.0.4

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.
@@ -9,7 +9,39 @@ function normalizeWindowsPath(input = "") {
9
9
  }
10
10
  return input.replace(/\\/g, "/").replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
11
11
  }
12
+
13
+ const _UNC_REGEX = /^[/\\]{2}/;
12
14
  const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
15
+ const _DRIVE_LETTER_RE = /^[A-Za-z]:$/;
16
+ const normalize = function(path) {
17
+ if (path.length === 0) {
18
+ return ".";
19
+ }
20
+ path = normalizeWindowsPath(path);
21
+ const isUNCPath = path.match(_UNC_REGEX);
22
+ const isPathAbsolute = isAbsolute(path);
23
+ const trailingSeparator = path[path.length - 1] === "/";
24
+ path = normalizeString(path, !isPathAbsolute);
25
+ if (path.length === 0) {
26
+ if (isPathAbsolute) {
27
+ return "/";
28
+ }
29
+ return trailingSeparator ? "./" : ".";
30
+ }
31
+ if (trailingSeparator) {
32
+ path += "/";
33
+ }
34
+ if (_DRIVE_LETTER_RE.test(path)) {
35
+ path += "/";
36
+ }
37
+ if (isUNCPath) {
38
+ if (!isPathAbsolute) {
39
+ return `//./${path}`;
40
+ }
41
+ return `//${path}`;
42
+ }
43
+ return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path;
44
+ };
13
45
  function cwd() {
14
46
  if (typeof process !== "undefined" && typeof process.cwd === "function") {
15
47
  return process.cwd().replace(/\\/g, "/");
@@ -115,4 +147,4 @@ async function writeCoverageFile(coverageFilesDirectory, coverage) {
115
147
  return filename;
116
148
  }
117
149
 
118
- export { commands as c, writeCoverageFile as w };
150
+ export { commands as c, normalize as n, writeCoverageFile as w };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import assert from 'node:assert';
2
2
  import { B as BaseCoverageProviderModule } from './base-BO8X8tRz.js';
3
- import { w as writeCoverageFile } from './commands-KXFl3PvR.js';
3
+ import { w as writeCoverageFile } from './commands-DzErh2po.js';
4
4
  import './constants-BCJfMgEg.js';
5
5
  import 'node:crypto';
6
6
  import 'node:fs';
@@ -9,8 +9,10 @@ declare class IstanbulCoverageProvider extends BaseCoverageProvider implements C
9
9
  version: string;
10
10
  instrumenter: Instrumenter;
11
11
  private transformedModuleIds;
12
+ private uncoveredFileCoverages;
12
13
  initialize(ctx: Vitest): void;
13
14
  requiresTransform(id: string): boolean;
15
+ clean(clean?: boolean): Promise<void>;
14
16
  onFileTransform(sourceCode: string, id: string, pluginCtx: Vite.Rollup.TransformPluginContext): {
15
17
  code: string;
16
18
  map: any;
package/dist/provider.js CHANGED
@@ -9,12 +9,12 @@ import { parseModule } from 'magicast';
9
9
  import { createDebug } from 'obug';
10
10
  import c from 'tinyrainbow';
11
11
  import { BaseCoverageProvider, isCSSRequest } from 'vitest/node';
12
- import { c as commands } from './commands-KXFl3PvR.js';
12
+ import { c as commands, n as normalize } from './commands-DzErh2po.js';
13
13
  import { C as COVERAGE_STORE_KEY } from './constants-BCJfMgEg.js';
14
14
  import 'node:crypto';
15
15
  import 'node:fs/promises';
16
16
 
17
- var version = "5.0.3";
17
+ var version = "5.0.4";
18
18
 
19
19
  const debug = createDebug("vitest:coverage");
20
20
  class IstanbulCoverageProvider extends BaseCoverageProvider {
@@ -22,6 +22,7 @@ class IstanbulCoverageProvider extends BaseCoverageProvider {
22
22
  version = version;
23
23
  instrumenter;
24
24
  transformedModuleIds = new Set();
25
+ uncoveredFileCoverages = new Map();
25
26
  initialize(ctx) {
26
27
  this._initialize(ctx);
27
28
  for (const project of ctx.projects) {
@@ -64,6 +65,10 @@ class IstanbulCoverageProvider extends BaseCoverageProvider {
64
65
  }
65
66
  return true;
66
67
  }
68
+ async clean(clean = true) {
69
+ await super.clean(clean);
70
+ this.uncoveredFileCoverages.clear();
71
+ }
67
72
  onFileTransform(sourceCode, id, pluginCtx) {
68
73
  if (!this.requiresTransform(id)) {
69
74
  return;
@@ -72,7 +77,10 @@ class IstanbulCoverageProvider extends BaseCoverageProvider {
72
77
  sourceMap.sources = sourceMap.sources.map(removeQueryParameters);
73
78
  sourceCode = sourceCode.replaceAll("_ts_decorate", "/* istanbul ignore next */_ts_decorate").replaceAll(/(if +\(import\.meta\.vitest\))/g, "/* istanbul ignore next */ $1");
74
79
  const code = this.instrumenter.instrumentSync(sourceCode, id, sourceMap);
75
- if (!id.includes("vitest-uncovered-coverage=true")) {
80
+ const uncoveredKey = getUncoveredKey(id);
81
+ if (uncoveredKey) {
82
+ this.uncoveredFileCoverages.set(uncoveredKey, this.instrumenter.lastFileCoverage());
83
+ } else {
76
84
  const transformMap = new GenMapping(sourceMap);
77
85
  eachMapping(new TraceMap(sourceMap), (mapping) => {
78
86
  addMapping(transformMap, {
@@ -131,6 +139,10 @@ class IstanbulCoverageProvider extends BaseCoverageProvider {
131
139
  }
132
140
  coverageMap.filter((filename) => {
133
141
  const exists = existsSync(filename);
142
+ // files can be instrumented before the changed files are resolved
143
+ if (this.changedFiles && !this.changedFiles.includes(normalize(filename))) {
144
+ return false;
145
+ }
134
146
  if (this.options.excludeAfterRemap) {
135
147
  return exists && this.isIncluded(filename);
136
148
  }
@@ -172,8 +184,6 @@ class IstanbulCoverageProvider extends BaseCoverageProvider {
172
184
  const cacheKey = Date.now();
173
185
  const coverageMap = this.createCoverageMap();
174
186
  const transform = this.createUncoveredFileTransformer(this.ctx);
175
- // Note that these cannot be run parallel as synchronous instrumenter.lastFileCoverage
176
- // returns the coverage of the last transformed file
177
187
  for (const [index, filename] of uncoveredFiles.entries()) {
178
188
  let timeout;
179
189
  let start;
@@ -182,10 +192,12 @@ class IstanbulCoverageProvider extends BaseCoverageProvider {
182
192
  timeout = setTimeout(() => debug(c.bgRed(`File "${filename}" is taking longer than 3s`)), 3e3);
183
193
  debug("Uncovered file %d/%d", index, uncoveredFiles.length);
184
194
  }
185
- // Make sure file is not served from cache so that instrumenter loads up requested file coverage
186
- await transform(`${filename}?cache=${cacheKey}&vitest-uncovered-coverage=true`);
187
- const lastCoverage = this.instrumenter.lastFileCoverage();
188
- coverageMap.addFileCoverage(lastCoverage);
195
+ // Unique key makes sure file is not served from cache
196
+ const uncoveredKey = `${cacheKey}-${index}`;
197
+ await transform(`${filename}?cache=${uncoveredKey}&vitest-uncovered-coverage=true`);
198
+ const fileCoverage = this.uncoveredFileCoverages.get(uncoveredKey) ?? this.instrumenter.lastFileCoverage();
199
+ this.uncoveredFileCoverages.delete(uncoveredKey);
200
+ coverageMap.addFileCoverage(fileCoverage);
189
201
  if (debug.enabled) {
190
202
  clearTimeout(timeout);
191
203
  const diff = performance.now() - start;
@@ -232,5 +244,11 @@ async function transformCoverage(coverageMap) {
232
244
  function removeQueryParameters(filename) {
233
245
  return filename.split("?")[0];
234
246
  }
247
+ function getUncoveredKey(id) {
248
+ if (!id.includes("vitest-uncovered-coverage=true")) {
249
+ return;
250
+ }
251
+ return new URLSearchParams(id.split("?")[1]).get("cache") ?? undefined;
252
+ }
235
253
 
236
254
  export { IstanbulCoverageProvider };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/coverage-istanbul",
3
3
  "type": "module",
4
- "version": "5.0.3",
4
+ "version": "5.0.4",
5
5
  "description": "Istanbul coverage provider for Vitest",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -41,22 +41,22 @@
41
41
  "dist"
42
42
  ],
43
43
  "peerDependencies": {
44
- "vitest": "5.0.3"
44
+ "vitest": "5.0.4"
45
45
  },
46
46
  "dependencies": {
47
47
  "@jridgewell/gen-mapping": "^0.3.13",
48
48
  "@jridgewell/trace-mapping": "0.3.31",
49
- "@vitest/istanbul-lib-coverage": "^1.0.0",
50
- "@vitest/istanbul-lib-instrument": "^1.0.0",
51
- "@vitest/istanbul-lib-report": "^1.0.0",
52
- "@vitest/istanbul-lib-source-maps": "^1.0.0",
53
- "magicast": "^0.5.4",
54
- "obug": "^2.1.4",
55
- "tinyrainbow": "^3.1.1"
49
+ "@vitest/istanbul-lib-coverage": "^1.0.2",
50
+ "@vitest/istanbul-lib-instrument": "^1.0.2",
51
+ "@vitest/istanbul-lib-report": "^1.0.2",
52
+ "@vitest/istanbul-lib-source-maps": "^1.0.2",
53
+ "magicast": "^0.5.5",
54
+ "obug": "^3.0.0",
55
+ "tinyrainbow": "^3.2.0"
56
56
  },
57
57
  "devDependencies": {
58
58
  "pathe": "^2.0.3",
59
- "vitest": "5.0.3"
59
+ "vitest": "5.0.4"
60
60
  },
61
61
  "scripts": {
62
62
  "build": "premove dist && rollup -c",