@vitest/coverage-v8 4.1.12 → 5.1.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.
- package/dist/browser.js +6 -38
- package/dist/commands-C6w5FFZp.js +204 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +49 -9
- package/dist/provider.d.ts +3 -1
- package/dist/provider.js +68 -31
- package/package.json +13 -18
- package/dist/pathe.M-eThtNZ-BTaAGrLg.js +0 -104
package/dist/browser.js
CHANGED
|
@@ -13,49 +13,17 @@ const mod = {
|
|
|
13
13
|
await triggerCommand("__vitest_startV8Coverage");
|
|
14
14
|
},
|
|
15
15
|
async takeCoverage() {
|
|
16
|
-
|
|
17
|
-
const result = [];
|
|
18
|
-
// Reduce amount of data sent over rpc by doing some early result filtering
|
|
19
|
-
for (const entry of coverage.result) {
|
|
20
|
-
if (filterResult(entry)) {
|
|
21
|
-
result.push({
|
|
22
|
-
...entry,
|
|
23
|
-
url: decodeURIComponent(entry.url.replace(window.location.origin, ""))
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return { result };
|
|
16
|
+
return triggerCommand("__vitest_takeV8Coverage", [window.location.href]);
|
|
28
17
|
},
|
|
29
|
-
stopCoverage() {
|
|
30
|
-
|
|
18
|
+
async stopCoverage({ isolate }) {
|
|
19
|
+
if (isolate === false) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
await triggerCommand("__vitest_stopV8Coverage");
|
|
31
23
|
},
|
|
32
24
|
async getProvider() {
|
|
33
25
|
return loadProvider();
|
|
34
26
|
}
|
|
35
27
|
};
|
|
36
|
-
function filterResult(coverage) {
|
|
37
|
-
if (!coverage.url.startsWith(window.location.origin)) {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
if (coverage.url.includes("/node_modules/")) {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
if (coverage.url.includes("__vitest_browser__")) {
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
if (coverage.url.includes("__vitest__/assets")) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
if (coverage.url === window.location.href) {
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
if (coverage.url.includes("/@id/@vitest/")) {
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
if (coverage.url.includes("/@vite/client")) {
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
28
|
|
|
61
29
|
export { mod as default };
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { writeFile } from 'node:fs/promises';
|
|
4
|
+
|
|
5
|
+
const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
|
|
6
|
+
function normalizeWindowsPath(input = "") {
|
|
7
|
+
if (!input) {
|
|
8
|
+
return input;
|
|
9
|
+
}
|
|
10
|
+
return input.replace(/\\/g, "/").replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const _UNC_REGEX = /^[/\\]{2}/;
|
|
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
|
+
};
|
|
45
|
+
function cwd() {
|
|
46
|
+
if (typeof process !== "undefined" && typeof process.cwd === "function") {
|
|
47
|
+
return process.cwd().replace(/\\/g, "/");
|
|
48
|
+
}
|
|
49
|
+
return "/";
|
|
50
|
+
}
|
|
51
|
+
const resolve = function(...arguments_) {
|
|
52
|
+
arguments_ = arguments_.map((argument) => normalizeWindowsPath(argument));
|
|
53
|
+
let resolvedPath = "";
|
|
54
|
+
let resolvedAbsolute = false;
|
|
55
|
+
for (let index = arguments_.length - 1; index >= -1 && !resolvedAbsolute; index--) {
|
|
56
|
+
const path = index >= 0 ? arguments_[index] : cwd();
|
|
57
|
+
if (!path || path.length === 0) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
resolvedPath = `${path}/${resolvedPath}`;
|
|
61
|
+
resolvedAbsolute = isAbsolute(path);
|
|
62
|
+
}
|
|
63
|
+
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
|
|
64
|
+
if (resolvedAbsolute && !isAbsolute(resolvedPath)) {
|
|
65
|
+
return `/${resolvedPath}`;
|
|
66
|
+
}
|
|
67
|
+
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
68
|
+
};
|
|
69
|
+
function normalizeString(path, allowAboveRoot) {
|
|
70
|
+
let res = "";
|
|
71
|
+
let lastSegmentLength = 0;
|
|
72
|
+
let lastSlash = -1;
|
|
73
|
+
let dots = 0;
|
|
74
|
+
let char = null;
|
|
75
|
+
for (let index = 0; index <= path.length; ++index) {
|
|
76
|
+
if (index < path.length) {
|
|
77
|
+
char = path[index];
|
|
78
|
+
} else if (char === "/") {
|
|
79
|
+
break;
|
|
80
|
+
} else {
|
|
81
|
+
char = "/";
|
|
82
|
+
}
|
|
83
|
+
if (char === "/") {
|
|
84
|
+
if (lastSlash === index - 1 || dots === 1) ; else if (dots === 2) {
|
|
85
|
+
if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
|
|
86
|
+
if (res.length > 2) {
|
|
87
|
+
const lastSlashIndex = res.lastIndexOf("/");
|
|
88
|
+
if (lastSlashIndex === -1) {
|
|
89
|
+
res = "";
|
|
90
|
+
lastSegmentLength = 0;
|
|
91
|
+
} else {
|
|
92
|
+
res = res.slice(0, lastSlashIndex);
|
|
93
|
+
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
|
94
|
+
}
|
|
95
|
+
lastSlash = index;
|
|
96
|
+
dots = 0;
|
|
97
|
+
continue;
|
|
98
|
+
} else if (res.length > 0) {
|
|
99
|
+
res = "";
|
|
100
|
+
lastSegmentLength = 0;
|
|
101
|
+
lastSlash = index;
|
|
102
|
+
dots = 0;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (allowAboveRoot) {
|
|
107
|
+
res += res.length > 0 ? "/.." : "..";
|
|
108
|
+
lastSegmentLength = 2;
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
if (res.length > 0) {
|
|
112
|
+
res += `/${path.slice(lastSlash + 1, index)}`;
|
|
113
|
+
} else {
|
|
114
|
+
res = path.slice(lastSlash + 1, index);
|
|
115
|
+
}
|
|
116
|
+
lastSegmentLength = index - lastSlash - 1;
|
|
117
|
+
}
|
|
118
|
+
lastSlash = index;
|
|
119
|
+
dots = 0;
|
|
120
|
+
} else if (char === "." && dots !== -1) {
|
|
121
|
+
++dots;
|
|
122
|
+
} else {
|
|
123
|
+
dots = -1;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return res;
|
|
127
|
+
}
|
|
128
|
+
const isAbsolute = function(p) {
|
|
129
|
+
return _IS_ABSOLUTE_RE.test(p);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const commands = {
|
|
133
|
+
startV8Coverage,
|
|
134
|
+
takeV8Coverage,
|
|
135
|
+
stopV8Coverage
|
|
136
|
+
};
|
|
137
|
+
async function startV8Coverage(context) {
|
|
138
|
+
const session = await context.__ensureCDPHandler();
|
|
139
|
+
// Previous test file's modules must be collected, otherwise V8 reuses their compiled code without block coverage
|
|
140
|
+
await session.send("HeapProfiler.collectGarbage");
|
|
141
|
+
await session.send("Profiler.enable");
|
|
142
|
+
await session.send("Profiler.startPreciseCoverage", {
|
|
143
|
+
callCount: true,
|
|
144
|
+
detailed: true
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
async function stopV8Coverage(context) {
|
|
148
|
+
const session = await context.__ensureCDPHandler();
|
|
149
|
+
await session.send("Profiler.stopPreciseCoverage");
|
|
150
|
+
await session.send("Profiler.disable");
|
|
151
|
+
}
|
|
152
|
+
async function takeV8Coverage(context, pageUrl) {
|
|
153
|
+
const session = await context.__ensureCDPHandler();
|
|
154
|
+
const coverage = await session.send("Profiler.takePreciseCoverage");
|
|
155
|
+
const origin = new URL(pageUrl).origin;
|
|
156
|
+
const result = [];
|
|
157
|
+
for (const entry of coverage.result) {
|
|
158
|
+
if (filterResult(entry.url, origin, pageUrl)) {
|
|
159
|
+
entry.url = decodeURIComponent(entry.url.replace(origin, ""));
|
|
160
|
+
result.push(entry);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const provider = context.project.vitest.coverageProvider;
|
|
164
|
+
return await writeCoverageFile(provider.coverageFilesDirectory, { result });
|
|
165
|
+
}
|
|
166
|
+
async function writeCoverageFile(coverageFilesDirectory, coverage) {
|
|
167
|
+
// Write results on file system directly and transfer only the filename over RPC
|
|
168
|
+
const filename = resolve(coverageFilesDirectory, `coverage-${randomUUID()}.json`);
|
|
169
|
+
try {
|
|
170
|
+
await writeFile(filename, JSON.stringify(coverage), "utf-8");
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (!existsSync(coverageFilesDirectory)) {
|
|
173
|
+
throw new Error(`Something removed the coverage directory "${coverageFilesDirectory}" Vitest created earlier. Make sure you are not running multiple Vitests with the same "coverage.reportsDirectory" at the same time.`, { cause: error });
|
|
174
|
+
}
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
return filename;
|
|
178
|
+
}
|
|
179
|
+
function filterResult(url, origin, pageUrl) {
|
|
180
|
+
if (!url.startsWith(origin)) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
if (url.includes("/node_modules/")) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
if (url.includes("__vitest_browser__")) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
if (url.includes("__vitest__/assets")) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
if (url === pageUrl) {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
if (url.includes("/@id/@vitest/")) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
if (url.includes("/@vite/client")) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export { commands as c, normalize as n, writeCoverageFile as w };
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,17 +1,29 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { existsSync } from 'node:fs';
|
|
4
|
+
import { readdir, readFile, rm } from 'node:fs/promises';
|
|
1
5
|
import inspector from 'node:inspector/promises';
|
|
6
|
+
import { resolve } from 'node:path';
|
|
2
7
|
import { fileURLToPath } from 'node:url';
|
|
3
8
|
import { provider } from 'std-env';
|
|
9
|
+
import { n as normalize, w as writeCoverageFile } from './commands-C6w5FFZp.js';
|
|
4
10
|
import { l as loadProvider } from './load-provider-CdgAx3rL.js';
|
|
5
|
-
import { n as normalize } from './pathe.M-eThtNZ-BTaAGrLg.js';
|
|
6
11
|
|
|
7
|
-
const session = new inspector.Session();
|
|
8
12
|
let enabled = false;
|
|
9
13
|
const mod = {
|
|
10
|
-
|
|
14
|
+
extendedContextCoverageDir: undefined,
|
|
15
|
+
session: null,
|
|
16
|
+
async startCoverage({ isolate, autoAttachSubprocess, reportsDirectory }) {
|
|
11
17
|
if (isolate === false && enabled) {
|
|
12
18
|
return;
|
|
13
19
|
}
|
|
14
20
|
enabled = true;
|
|
21
|
+
if (autoAttachSubprocess) {
|
|
22
|
+
this.extendedContextCoverageDir = resolve(reportsDirectory, ".tmp", randomUUID());
|
|
23
|
+
process.env.NODE_V8_COVERAGE = this.extendedContextCoverageDir;
|
|
24
|
+
}
|
|
25
|
+
this.session ||= new inspector.Session();
|
|
26
|
+
const session = this.session;
|
|
15
27
|
session.connect();
|
|
16
28
|
await session.post("Profiler.enable");
|
|
17
29
|
await session.post("Profiler.startPreciseCoverage", {
|
|
@@ -21,28 +33,56 @@ const mod = {
|
|
|
21
33
|
},
|
|
22
34
|
async takeCoverage(options) {
|
|
23
35
|
if (provider === "stackblitz") {
|
|
24
|
-
return
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const session = this.session;
|
|
39
|
+
if (!session) {
|
|
40
|
+
throw new Error("V8 provider missing inspector session.");
|
|
25
41
|
}
|
|
26
42
|
const coverage = await session.post("Profiler.takePreciseCoverage");
|
|
27
43
|
const result = [];
|
|
28
44
|
// Reduce amount of data sent over rpc by doing some early result filtering
|
|
29
45
|
for (const entry of coverage.result) {
|
|
30
46
|
if (filterResult(entry)) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
startOffset: options?.moduleExecutionInfo?.get(normalize(fileURLToPath(entry.url)))?.startOffset || 0
|
|
34
|
-
});
|
|
47
|
+
entry.startOffset = options?.moduleExecutionInfo?.get(normalize(fileURLToPath(entry.url)))?.startOffset || 0;
|
|
48
|
+
result.push(entry);
|
|
35
49
|
}
|
|
36
50
|
}
|
|
37
|
-
|
|
51
|
+
if (this.extendedContextCoverageDir && existsSync(this.extendedContextCoverageDir)) {
|
|
52
|
+
const filenames = await readdir(this.extendedContextCoverageDir);
|
|
53
|
+
const contents = await Promise.all(filenames.filter((filename) => filename.endsWith(".json")).map(async (filename) => {
|
|
54
|
+
const path = `${this.extendedContextCoverageDir}/${filename}`;
|
|
55
|
+
const content = await readFile(path, "utf8");
|
|
56
|
+
await rm(path);
|
|
57
|
+
return content;
|
|
58
|
+
}));
|
|
59
|
+
for (const content of contents) {
|
|
60
|
+
const json = JSON.parse(content);
|
|
61
|
+
for (const entry of json.result) {
|
|
62
|
+
if (filterResult(entry)) {
|
|
63
|
+
entry.startOffset = 0;
|
|
64
|
+
entry.isExtendedContext = true;
|
|
65
|
+
result.push(entry);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const coverageFilesDirectory = options?.coverageFilesDirectory;
|
|
71
|
+
assert(coverageFilesDirectory, "coverageFilesDirectory is required");
|
|
72
|
+
return await writeCoverageFile(coverageFilesDirectory, { result });
|
|
38
73
|
},
|
|
39
74
|
async stopCoverage({ isolate }) {
|
|
40
75
|
if (isolate === false) {
|
|
41
76
|
return;
|
|
42
77
|
}
|
|
78
|
+
const session = this.session;
|
|
79
|
+
if (!session) {
|
|
80
|
+
throw new Error("V8 provider missing inspector session.");
|
|
81
|
+
}
|
|
43
82
|
await session.post("Profiler.stopPreciseCoverage");
|
|
44
83
|
await session.post("Profiler.disable");
|
|
45
84
|
session.disconnect();
|
|
85
|
+
this.session = null;
|
|
46
86
|
},
|
|
47
87
|
async getProvider() {
|
|
48
88
|
return loadProvider();
|
package/dist/provider.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { CoverageMap } from 'istanbul-lib-coverage';
|
|
1
|
+
import { CoverageMap } from '@vitest/istanbul-lib-coverage';
|
|
2
2
|
import { ProxifiedModule } from 'magicast';
|
|
3
3
|
import { Profiler } from 'node:inspector';
|
|
4
4
|
import { BaseCoverageProvider, CoverageProvider, Vitest, ReportContext } from 'vitest/node';
|
|
5
5
|
|
|
6
6
|
interface ScriptCoverageWithOffset extends Profiler.ScriptCoverage {
|
|
7
7
|
startOffset: number;
|
|
8
|
+
/** Whether script ran outside Vite, e.g. in sub-processes or worker threads */
|
|
9
|
+
isExtendedContext?: boolean;
|
|
8
10
|
}
|
|
9
11
|
declare class V8CoverageProvider extends BaseCoverageProvider implements CoverageProvider {
|
|
10
12
|
name: "v8";
|
package/dist/provider.js
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import { existsSync, promises } from 'node:fs';
|
|
2
2
|
import { fileURLToPath } from 'node:url';
|
|
3
|
-
import {
|
|
3
|
+
import { mergeScriptCovs } from '@bcoe/v8-coverage';
|
|
4
|
+
import * as libCoverage from '@vitest/istanbul-lib-coverage';
|
|
5
|
+
import * as libReport from '@vitest/istanbul-lib-report';
|
|
4
6
|
import astV8ToIstanbul from 'ast-v8-to-istanbul';
|
|
5
|
-
import libCoverage from 'istanbul-lib-coverage';
|
|
6
|
-
import libReport from 'istanbul-lib-report';
|
|
7
|
-
import reports from 'istanbul-reports';
|
|
8
7
|
import { parseModule } from 'magicast';
|
|
9
8
|
import { createDebug } from 'obug';
|
|
10
9
|
import { provider } from 'std-env';
|
|
11
10
|
import c from 'tinyrainbow';
|
|
12
11
|
import { BaseCoverageProvider, parseAstAsync } from 'vitest/node';
|
|
13
|
-
import { n as normalize } from './
|
|
12
|
+
import { c as commands, n as normalize } from './commands-C6w5FFZp.js';
|
|
13
|
+
import 'node:crypto';
|
|
14
|
+
import 'node:fs/promises';
|
|
14
15
|
|
|
15
|
-
var version = "
|
|
16
|
+
var version = "5.1.0";
|
|
16
17
|
|
|
17
18
|
const FILE_PROTOCOL = "file://";
|
|
18
19
|
const debug = createDebug("vitest:coverage");
|
|
@@ -21,6 +22,23 @@ class V8CoverageProvider extends BaseCoverageProvider {
|
|
|
21
22
|
version = version;
|
|
22
23
|
initialize(ctx) {
|
|
23
24
|
this._initialize(ctx);
|
|
25
|
+
for (const project of ctx.projects) {
|
|
26
|
+
if (project.isBrowserEnabled() && project.browser) {
|
|
27
|
+
for (const [name, command] of Object.entries(commands)) {
|
|
28
|
+
project.browser.registerCommand(`__vitest_${name}`, command);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (this.options.autoAttachSubprocess) {
|
|
33
|
+
const isAnyThreadsPools = ctx.projects.some((p) => p.config.pool === "threads" || p.config.pool === "vmThreads");
|
|
34
|
+
if (isAnyThreadsPools) {
|
|
35
|
+
// Work-around for https://github.com/nodejs/node/issues/46378
|
|
36
|
+
// Node never does anything with this directory, it's just required so that
|
|
37
|
+
// the next Workers read **their** env.NODE_V8_COVERAGE.
|
|
38
|
+
// Node never creates this .unused directory at all.
|
|
39
|
+
process.env.NODE_V8_COVERAGE = `${this.coverageFilesDirectory}/.unused`;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
24
42
|
}
|
|
25
43
|
createCoverageMap() {
|
|
26
44
|
return libCoverage.createCoverageMap({});
|
|
@@ -28,24 +46,28 @@ class V8CoverageProvider extends BaseCoverageProvider {
|
|
|
28
46
|
async generateCoverage({ allTestsRun }) {
|
|
29
47
|
const start = debug.enabled ? performance.now() : 0;
|
|
30
48
|
const coverageMap = this.createCoverageMap();
|
|
31
|
-
|
|
49
|
+
const mergedScripts = new Map();
|
|
50
|
+
const autoAttachSubprocess = this.options.autoAttachSubprocess;
|
|
32
51
|
await this.readCoverageFiles({
|
|
33
52
|
onFileRead(coverage) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
53
|
+
for (const script of coverage.result) {
|
|
54
|
+
const previous = mergedScripts.get(script.url);
|
|
55
|
+
const merged = mergeScriptCovs(previous ? [previous, script] : [script]);
|
|
56
|
+
const startOffset = previous?.startOffset || script.startOffset || 0;
|
|
57
|
+
const isExtendedContext = previous?.isExtendedContext || script.isExtendedContext;
|
|
58
|
+
merged.startOffset ||= startOffset;
|
|
59
|
+
if (autoAttachSubprocess && isExtendedContext) {
|
|
60
|
+
merged.isExtendedContext = true;
|
|
40
61
|
}
|
|
41
|
-
|
|
62
|
+
mergedScripts.set(merged.url, merged);
|
|
63
|
+
}
|
|
42
64
|
},
|
|
43
65
|
onFinished: async (project, environment) => {
|
|
44
66
|
// Source maps can change based on projectName and transform mode.
|
|
45
67
|
// Coverage transform re-uses source maps so we need to separate transforms from each other.
|
|
46
|
-
const converted = await this.convertCoverage(
|
|
68
|
+
const converted = await this.convertCoverage({ result: Array.from(mergedScripts.values()) }, project, environment);
|
|
47
69
|
coverageMap.merge(converted);
|
|
48
|
-
|
|
70
|
+
mergedScripts.clear();
|
|
49
71
|
},
|
|
50
72
|
onDebug: debug
|
|
51
73
|
});
|
|
@@ -82,11 +104,12 @@ class V8CoverageProvider extends BaseCoverageProvider {
|
|
|
82
104
|
}
|
|
83
105
|
for (const reporter of this.options.reporter) {
|
|
84
106
|
// Type assertion required for custom reporters
|
|
85
|
-
|
|
107
|
+
const reportInstance = await libReport.createAsync(reporter[0], {
|
|
86
108
|
skipFull: this.options.skipFull,
|
|
87
109
|
projectRoot: this.ctx.config.root,
|
|
88
110
|
...reporter[1]
|
|
89
|
-
})
|
|
111
|
+
});
|
|
112
|
+
reportInstance.execute(context);
|
|
90
113
|
}
|
|
91
114
|
if (this.options.thresholds) {
|
|
92
115
|
await this.reportThresholds(coverageMap, allTestsRun);
|
|
@@ -150,6 +173,10 @@ class V8CoverageProvider extends BaseCoverageProvider {
|
|
|
150
173
|
if (type === "statement" && node.type === "VariableDeclarator" && node.id.type === "Identifier" && node.id.name.startsWith("__vite_ssr_import_")) {
|
|
151
174
|
return true;
|
|
152
175
|
}
|
|
176
|
+
// SSR transformed import binding
|
|
177
|
+
if (type === "statement" && node.type === "VariableDeclarator" && node.init && node.init.type === "MemberExpression" && node.init.object.type === "MemberExpression" && node.init.object.object.type === "Identifier" && node.init.object.object.name.startsWith("__vite_ssr_import_")) {
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
153
180
|
// SSR transformed exports vite@>6.3.5
|
|
154
181
|
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__") {
|
|
155
182
|
return true;
|
|
@@ -163,6 +190,11 @@ class V8CoverageProvider extends BaseCoverageProvider {
|
|
|
163
190
|
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") {
|
|
164
191
|
return true;
|
|
165
192
|
}
|
|
193
|
+
// CJS namespace imports wrapped in interop helper - e.g.
|
|
194
|
+
// const React = ((m, n) => n || !m?.__esModule ? { ...m, default: m } : m)(__vite__cjsImport0_react, 1);
|
|
195
|
+
if (type === "statement" && node.type === "VariableDeclarator" && node.init?.type === "CallExpression" && node.init.callee.type === "ArrowFunctionExpression" && node.init.arguments[0]?.type === "Identifier" && node.init.arguments[0].name.startsWith("__vite__cjsImport")) {
|
|
196
|
+
return "ignore-this-and-nested-nodes";
|
|
197
|
+
}
|
|
166
198
|
// in-source test with "if (import.meta.vitest)"
|
|
167
199
|
if ((type === "branch" || type === "statement") && node.type === "IfStatement" && node.test.type === "MemberExpression" && node.test.property.type === "Identifier" && node.test.property.name === "vitest") {
|
|
168
200
|
// SSR
|
|
@@ -186,14 +218,15 @@ class V8CoverageProvider extends BaseCoverageProvider {
|
|
|
186
218
|
if (type === "statement" && node.type === "ExpressionStatement" && node.expression.type === "CallExpression" && node.expression.callee.type === "Identifier" && node.expression.callee.name === "_ts_decorate") {
|
|
187
219
|
return "ignore-this-and-nested-nodes";
|
|
188
220
|
}
|
|
221
|
+
return this.options.ignoreNode?.(node, type);
|
|
189
222
|
}
|
|
190
223
|
});
|
|
191
224
|
}
|
|
192
|
-
async getSources(url, onTransform, functions = []) {
|
|
225
|
+
async getSources(url, onTransform, functions = [], isExtendedContext = false) {
|
|
193
226
|
// TODO: need to standardize file urls before this call somehow, this is messy
|
|
194
|
-
const filepath =
|
|
227
|
+
const filepath = /^file:\/\/\/\w:\//.test(url) ? url.slice(8) : removeStartsWith(url, FILE_PROTOCOL);
|
|
195
228
|
// TODO: do we still need to "catch" here? why would it fail?
|
|
196
|
-
const transformResult = await onTransform(filepath).catch(() => null);
|
|
229
|
+
const transformResult = await onTransform(filepath, isExtendedContext).catch(() => null);
|
|
197
230
|
const map = transformResult?.map;
|
|
198
231
|
const code = transformResult?.code;
|
|
199
232
|
if (code == null) {
|
|
@@ -220,12 +253,9 @@ class V8CoverageProvider extends BaseCoverageProvider {
|
|
|
220
253
|
};
|
|
221
254
|
}
|
|
222
255
|
async convertCoverage(coverage, project = this.ctx.getRootProject(), environment) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const onTransform = async (filepath) => {
|
|
227
|
-
const result = await this.transformFile(filepath, project, environment);
|
|
228
|
-
if (result && environment === "__browser__" && project.browser) {
|
|
256
|
+
const onTransform = async (filepath, isExtendedContext = false) => {
|
|
257
|
+
const result = await this.transformFile(filepath, project, environment, !isExtendedContext);
|
|
258
|
+
if (result && project.isBrowserEnabled()) {
|
|
229
259
|
return {
|
|
230
260
|
...result,
|
|
231
261
|
code: `${result.code}// <inline-source-map>`
|
|
@@ -235,7 +265,7 @@ class V8CoverageProvider extends BaseCoverageProvider {
|
|
|
235
265
|
};
|
|
236
266
|
const scriptCoverages = [];
|
|
237
267
|
for (const result of coverage.result) {
|
|
238
|
-
if (environment === "
|
|
268
|
+
if (environment === "client" && project.isBrowserEnabled()) {
|
|
239
269
|
if (result.url.startsWith("/@fs")) {
|
|
240
270
|
result.url = `${FILE_PROTOCOL}${removeStartsWith(result.url, "/@fs")}`;
|
|
241
271
|
} else if (result.url.startsWith(project.config.root)) {
|
|
@@ -244,7 +274,14 @@ class V8CoverageProvider extends BaseCoverageProvider {
|
|
|
244
274
|
result.url = `${FILE_PROTOCOL}${project.config.root}${result.url}`;
|
|
245
275
|
}
|
|
246
276
|
}
|
|
247
|
-
|
|
277
|
+
let filePath;
|
|
278
|
+
// Skip results that cannot be converted to file paths, for example virtual files that don't prefix to project root
|
|
279
|
+
try {
|
|
280
|
+
filePath = fileURLToPath(result.url);
|
|
281
|
+
} catch {
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
if (this.isIncluded(filePath)) {
|
|
248
285
|
scriptCoverages.push({
|
|
249
286
|
...result,
|
|
250
287
|
url: decodeURIComponent(result.url)
|
|
@@ -258,14 +295,14 @@ class V8CoverageProvider extends BaseCoverageProvider {
|
|
|
258
295
|
index += chunk.length;
|
|
259
296
|
debug("Converting %d/%d", index, scriptCoverages.length);
|
|
260
297
|
}
|
|
261
|
-
await Promise.all(chunk.map(async ({ url, functions, startOffset }) => {
|
|
298
|
+
await Promise.all(chunk.map(async ({ url, functions, startOffset, isExtendedContext }) => {
|
|
262
299
|
let timeout;
|
|
263
300
|
let start;
|
|
264
301
|
if (debug.enabled) {
|
|
265
302
|
start = performance.now();
|
|
266
303
|
timeout = setTimeout(() => debug(c.bgRed(`File "${fileURLToPath(url)}" is taking longer than 3s`)), 3e3);
|
|
267
304
|
}
|
|
268
|
-
const sources = await this.getSources(url, onTransform, functions);
|
|
305
|
+
const sources = await this.getSources(url, onTransform, functions, isExtendedContext);
|
|
269
306
|
coverageMap.merge(await this.remapCoverage(url, startOffset, sources, functions));
|
|
270
307
|
if (debug.enabled) {
|
|
271
308
|
clearTimeout(timeout);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitest/coverage-v8",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.1.0",
|
|
5
5
|
"description": "V8 coverage provider for Vitest",
|
|
6
6
|
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -41,8 +41,8 @@
|
|
|
41
41
|
"dist"
|
|
42
42
|
],
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@vitest/browser": "
|
|
45
|
-
"vitest": "
|
|
44
|
+
"@vitest/browser": "5.1.0",
|
|
45
|
+
"vitest": "5.1.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependenciesMeta": {
|
|
48
48
|
"@vitest/browser": {
|
|
@@ -51,24 +51,19 @@
|
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"@bcoe/v8-coverage": "^1.0.2",
|
|
54
|
-
"
|
|
55
|
-
"istanbul-lib-
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"tinyrainbow": "^3.1.0",
|
|
62
|
-
"@vitest/utils": "4.1.12"
|
|
54
|
+
"@vitest/istanbul-lib-coverage": "^2.1.0",
|
|
55
|
+
"@vitest/istanbul-lib-report": "^2.1.0",
|
|
56
|
+
"ast-v8-to-istanbul": "^1.0.7",
|
|
57
|
+
"magicast": "^0.5.5",
|
|
58
|
+
"obug": "^3.0.0",
|
|
59
|
+
"std-env": "^4.3.0",
|
|
60
|
+
"tinyrainbow": "^3.2.0"
|
|
63
61
|
},
|
|
64
62
|
"devDependencies": {
|
|
65
|
-
"@
|
|
66
|
-
"@
|
|
67
|
-
"@types/istanbul-reports": "^3.0.4",
|
|
63
|
+
"@vitest/browser": "5.1.0",
|
|
64
|
+
"@vitest/browser-playwright": "5.1.0",
|
|
68
65
|
"pathe": "^2.0.3",
|
|
69
|
-
"
|
|
70
|
-
"@vitest/browser-playwright": "4.1.12",
|
|
71
|
-
"vitest": "4.1.12"
|
|
66
|
+
"vitest": "5.1.0"
|
|
72
67
|
},
|
|
73
68
|
"scripts": {
|
|
74
69
|
"build": "premove dist && rollup -c",
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
|
|
2
|
-
function normalizeWindowsPath(input = "") {
|
|
3
|
-
if (!input) {
|
|
4
|
-
return input;
|
|
5
|
-
}
|
|
6
|
-
return input.replace(/\\/g, "/").replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const _UNC_REGEX = /^[/\\]{2}/;
|
|
10
|
-
const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
|
|
11
|
-
const _DRIVE_LETTER_RE = /^[A-Za-z]:$/;
|
|
12
|
-
const normalize = function(path) {
|
|
13
|
-
if (path.length === 0) {
|
|
14
|
-
return ".";
|
|
15
|
-
}
|
|
16
|
-
path = normalizeWindowsPath(path);
|
|
17
|
-
const isUNCPath = path.match(_UNC_REGEX);
|
|
18
|
-
const isPathAbsolute = isAbsolute(path);
|
|
19
|
-
const trailingSeparator = path[path.length - 1] === "/";
|
|
20
|
-
path = normalizeString(path, !isPathAbsolute);
|
|
21
|
-
if (path.length === 0) {
|
|
22
|
-
if (isPathAbsolute) {
|
|
23
|
-
return "/";
|
|
24
|
-
}
|
|
25
|
-
return trailingSeparator ? "./" : ".";
|
|
26
|
-
}
|
|
27
|
-
if (trailingSeparator) {
|
|
28
|
-
path += "/";
|
|
29
|
-
}
|
|
30
|
-
if (_DRIVE_LETTER_RE.test(path)) {
|
|
31
|
-
path += "/";
|
|
32
|
-
}
|
|
33
|
-
if (isUNCPath) {
|
|
34
|
-
if (!isPathAbsolute) {
|
|
35
|
-
return `//./${path}`;
|
|
36
|
-
}
|
|
37
|
-
return `//${path}`;
|
|
38
|
-
}
|
|
39
|
-
return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path;
|
|
40
|
-
};
|
|
41
|
-
function normalizeString(path, allowAboveRoot) {
|
|
42
|
-
let res = "";
|
|
43
|
-
let lastSegmentLength = 0;
|
|
44
|
-
let lastSlash = -1;
|
|
45
|
-
let dots = 0;
|
|
46
|
-
let char = null;
|
|
47
|
-
for (let index = 0; index <= path.length; ++index) {
|
|
48
|
-
if (index < path.length) {
|
|
49
|
-
char = path[index];
|
|
50
|
-
} else if (char === "/") {
|
|
51
|
-
break;
|
|
52
|
-
} else {
|
|
53
|
-
char = "/";
|
|
54
|
-
}
|
|
55
|
-
if (char === "/") {
|
|
56
|
-
if (lastSlash === index - 1 || dots === 1) ; else if (dots === 2) {
|
|
57
|
-
if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
|
|
58
|
-
if (res.length > 2) {
|
|
59
|
-
const lastSlashIndex = res.lastIndexOf("/");
|
|
60
|
-
if (lastSlashIndex === -1) {
|
|
61
|
-
res = "";
|
|
62
|
-
lastSegmentLength = 0;
|
|
63
|
-
} else {
|
|
64
|
-
res = res.slice(0, lastSlashIndex);
|
|
65
|
-
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
|
66
|
-
}
|
|
67
|
-
lastSlash = index;
|
|
68
|
-
dots = 0;
|
|
69
|
-
continue;
|
|
70
|
-
} else if (res.length > 0) {
|
|
71
|
-
res = "";
|
|
72
|
-
lastSegmentLength = 0;
|
|
73
|
-
lastSlash = index;
|
|
74
|
-
dots = 0;
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
if (allowAboveRoot) {
|
|
79
|
-
res += res.length > 0 ? "/.." : "..";
|
|
80
|
-
lastSegmentLength = 2;
|
|
81
|
-
}
|
|
82
|
-
} else {
|
|
83
|
-
if (res.length > 0) {
|
|
84
|
-
res += `/${path.slice(lastSlash + 1, index)}`;
|
|
85
|
-
} else {
|
|
86
|
-
res = path.slice(lastSlash + 1, index);
|
|
87
|
-
}
|
|
88
|
-
lastSegmentLength = index - lastSlash - 1;
|
|
89
|
-
}
|
|
90
|
-
lastSlash = index;
|
|
91
|
-
dots = 0;
|
|
92
|
-
} else if (char === "." && dots !== -1) {
|
|
93
|
-
++dots;
|
|
94
|
-
} else {
|
|
95
|
-
dots = -1;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return res;
|
|
99
|
-
}
|
|
100
|
-
const isAbsolute = function(p) {
|
|
101
|
-
return _IS_ABSOLUTE_RE.test(p);
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
export { normalize as n };
|