supercov 0.0.0 → 0.0.2
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/bin/supercov.js +2 -5
- package/dist/analyze.d.ts +12 -0
- package/dist/analyze.d.ts.map +1 -0
- package/dist/analyze.js +699 -0
- package/dist/analyze.js.map +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +245 -0
- package/dist/cli.js.map +1 -0
- package/dist/instrumenter.d.ts +11 -0
- package/dist/instrumenter.d.ts.map +1 -0
- package/dist/instrumenter.js +940 -0
- package/dist/instrumenter.js.map +1 -0
- package/dist/integrity.d.ts +10 -0
- package/dist/integrity.d.ts.map +1 -0
- package/dist/integrity.js +153 -0
- package/dist/integrity.js.map +1 -0
- package/dist/playwright.d.ts +13 -0
- package/dist/playwright.d.ts.map +1 -0
- package/dist/playwright.js +794 -0
- package/dist/playwright.js.map +1 -0
- package/dist/playwrightReporter.d.ts +6 -0
- package/dist/playwrightReporter.d.ts.map +1 -0
- package/dist/playwrightReporter.js +40 -0
- package/dist/playwrightReporter.js.map +1 -0
- package/dist/project.d.ts +11 -0
- package/dist/project.d.ts.map +1 -0
- package/dist/project.js +112 -0
- package/dist/project.js.map +1 -0
- package/dist/provenance.d.ts +9 -0
- package/dist/provenance.d.ts.map +1 -0
- package/dist/provenance.js +51 -0
- package/dist/provenance.js.map +1 -0
- package/dist/query.d.ts +9 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +911 -0
- package/dist/query.js.map +1 -0
- package/dist/queueAdapters.d.ts +15 -0
- package/dist/queueAdapters.d.ts.map +1 -0
- package/dist/queueAdapters.js +55 -0
- package/dist/queueAdapters.js.map +1 -0
- package/dist/register.d.mts +2 -0
- package/dist/register.d.mts.map +1 -0
- package/dist/register.mjs +112 -0
- package/dist/register.mjs.map +1 -0
- package/dist/reporter.d.ts +17 -0
- package/dist/reporter.d.ts.map +1 -0
- package/dist/reporter.js +311 -0
- package/dist/reporter.js.map +1 -0
- package/dist/resolve-loader.d.mts +2 -0
- package/dist/resolve-loader.d.mts.map +1 -0
- package/dist/resolve-loader.mjs +40 -0
- package/dist/resolve-loader.mjs.map +1 -0
- package/dist/runtime.d.ts +46 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +480 -0
- package/dist/runtime.js.map +1 -0
- package/dist/transport.d.ts +15 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +100 -0
- package/dist/transport.js.map +1 -0
- package/dist/types.d.ts +353 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/vitePlugin.d.ts +8 -0
- package/dist/vitePlugin.d.ts.map +1 -0
- package/dist/vitePlugin.js +83 -0
- package/dist/vitePlugin.js.map +1 -0
- package/dist/vitest.d.ts +2 -0
- package/dist/vitest.d.ts.map +1 -0
- package/dist/vitest.js +89 -0
- package/dist/vitest.js.map +1 -0
- package/dist/vitestReporter.d.ts +28 -0
- package/dist/vitestReporter.d.ts.map +1 -0
- package/dist/vitestReporter.js +48 -0
- package/dist/vitestReporter.js.map +1 -0
- package/package.json +21 -9
- package/src/analyze.ts +0 -993
- package/src/cli.ts +0 -336
- package/src/instrumenter.ts +0 -1254
- package/src/integrity.ts +0 -187
- package/src/playwright.ts +0 -1009
- package/src/playwrightReporter.ts +0 -55
- package/src/project.ts +0 -149
- package/src/provenance.ts +0 -69
- package/src/query.ts +0 -1354
- package/src/queueAdapters.ts +0 -104
- package/src/register.mjs +0 -123
- package/src/reporter.ts +0 -431
- package/src/resolve-loader.mjs +0 -45
- package/src/runtime.ts +0 -656
- package/src/transport.ts +0 -132
- package/src/types.ts +0 -412
- package/src/vitePlugin.ts +0 -121
- package/src/vitest.ts +0 -101
- package/src/vitestReporter.ts +0 -72
|
@@ -0,0 +1,794 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import childProcess from "node:child_process";
|
|
11
|
+
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
12
|
+
import http from "node:http";
|
|
13
|
+
import https from "node:https";
|
|
14
|
+
import { syncBuiltinESMExports } from "node:module";
|
|
15
|
+
import { dirname, relative, resolve, sep } from "node:path";
|
|
16
|
+
import * as standardPlaywright from "@playwright/test";
|
|
17
|
+
import { inferTestProvenance } from "./provenance.js";
|
|
18
|
+
import { COVERAGE_PHASE_HEADER, COVERAGE_SCOPE_HEADER, COVERAGE_CARRIER_ENV, encodeCoverageCarrier, encodeCoverageScope, serverEvidenceDirectory, serverEvidencePath, } from "./transport.js";
|
|
19
|
+
export * from "@playwright/test";
|
|
20
|
+
const generatedTargetModule = "__SUPERCOV_PLAYWRIGHT_MODULE__";
|
|
21
|
+
const targetModule = process.env["SUPERCOV_PLAYWRIGHT_MODULE"] ??
|
|
22
|
+
(generatedTargetModule.startsWith("__")
|
|
23
|
+
? "@playwright/test"
|
|
24
|
+
: generatedTargetModule);
|
|
25
|
+
const adapter = (targetModule === "@playwright/test"
|
|
26
|
+
? standardPlaywright
|
|
27
|
+
: await import(__rewriteRelativeImportExtension(targetModule)));
|
|
28
|
+
const base = (adapter.offlineTest ?? adapter.test);
|
|
29
|
+
const baseExpect = adapter.expect;
|
|
30
|
+
const GENERATED_EVIDENCE_DIRECTORY = "__SUPERCOV_EVIDENCE_DIRECTORY__";
|
|
31
|
+
const GENERATED_RUN_ID = "__SUPERCOV_RUN_ID__";
|
|
32
|
+
const PHASE_STORAGE_KEY = "__supercov_phase";
|
|
33
|
+
const ACTION_METHODS = new Set([
|
|
34
|
+
"blur",
|
|
35
|
+
"check",
|
|
36
|
+
"click",
|
|
37
|
+
"dblclick",
|
|
38
|
+
"dispatchEvent",
|
|
39
|
+
"dragTo",
|
|
40
|
+
"evaluate",
|
|
41
|
+
"evaluateHandle",
|
|
42
|
+
"fill",
|
|
43
|
+
"focus",
|
|
44
|
+
"goBack",
|
|
45
|
+
"goForward",
|
|
46
|
+
"goto",
|
|
47
|
+
"hover",
|
|
48
|
+
"press",
|
|
49
|
+
"reload",
|
|
50
|
+
"selectOption",
|
|
51
|
+
"setInputFiles",
|
|
52
|
+
"tap",
|
|
53
|
+
"type",
|
|
54
|
+
"uncheck",
|
|
55
|
+
]);
|
|
56
|
+
const REQUEST_METHODS = new Set([
|
|
57
|
+
"delete",
|
|
58
|
+
"fetch",
|
|
59
|
+
"get",
|
|
60
|
+
"head",
|
|
61
|
+
"patch",
|
|
62
|
+
"post",
|
|
63
|
+
"put",
|
|
64
|
+
]);
|
|
65
|
+
function isApiRequestContext(value) {
|
|
66
|
+
const candidate = value;
|
|
67
|
+
return (typeof candidate["fetch"] === "function" &&
|
|
68
|
+
typeof candidate["get"] === "function" &&
|
|
69
|
+
typeof candidate["post"] === "function" &&
|
|
70
|
+
typeof candidate["storageState"] === "function");
|
|
71
|
+
}
|
|
72
|
+
function callerSource() {
|
|
73
|
+
const stack = new Error().stack?.split("\n").slice(2) ?? [];
|
|
74
|
+
const candidate = stack.find((line) => /[/\\]tests[/\\]/.test(line) &&
|
|
75
|
+
!line.includes(".supercov") &&
|
|
76
|
+
!line.includes("node_modules"));
|
|
77
|
+
if (!candidate)
|
|
78
|
+
return undefined;
|
|
79
|
+
return candidate
|
|
80
|
+
.trim()
|
|
81
|
+
.replace(/^at\s+/, "")
|
|
82
|
+
.replace("file:///workspace/", "")
|
|
83
|
+
.replace("/workspace/", "");
|
|
84
|
+
}
|
|
85
|
+
class CoveragePhaseController {
|
|
86
|
+
scope;
|
|
87
|
+
configuredHeaders;
|
|
88
|
+
phases = [];
|
|
89
|
+
counter = 0;
|
|
90
|
+
lastActionId;
|
|
91
|
+
activePhaseId;
|
|
92
|
+
pages = new Set();
|
|
93
|
+
workers = new Set();
|
|
94
|
+
contexts = new Set();
|
|
95
|
+
contextConfiguredHeaders = new Map();
|
|
96
|
+
contextRoutes = new Map();
|
|
97
|
+
cdpSessions = new Map();
|
|
98
|
+
newDocumentScriptIds = new Map();
|
|
99
|
+
pendingRegistrations = new Set();
|
|
100
|
+
scriptUpdate = Promise.resolve();
|
|
101
|
+
proxyCache = new WeakMap();
|
|
102
|
+
// Parameter properties are stateful despite the base ESLint rule treating
|
|
103
|
+
// this as an empty constructor.
|
|
104
|
+
// eslint-disable-next-line no-useless-constructor
|
|
105
|
+
constructor(scope, configuredHeaders = {}) {
|
|
106
|
+
this.scope = scope;
|
|
107
|
+
this.configuredHeaders = configuredHeaders;
|
|
108
|
+
}
|
|
109
|
+
allPages() {
|
|
110
|
+
return [...this.pages];
|
|
111
|
+
}
|
|
112
|
+
allWorkers() {
|
|
113
|
+
return [...this.workers];
|
|
114
|
+
}
|
|
115
|
+
async registerPage(page) {
|
|
116
|
+
if (this.pages.has(page))
|
|
117
|
+
return;
|
|
118
|
+
this.pages.add(page);
|
|
119
|
+
await this.registerContext(page.context());
|
|
120
|
+
const cdp = await page.context().newCDPSession(page).catch(() => undefined);
|
|
121
|
+
if (cdp)
|
|
122
|
+
this.cdpSessions.set(page, cdp);
|
|
123
|
+
const phaseId = this.requestPhaseId();
|
|
124
|
+
if (phaseId)
|
|
125
|
+
await this.activatePage(page, phaseId);
|
|
126
|
+
page.on("worker", (worker) => {
|
|
127
|
+
void this.registerWorker(worker);
|
|
128
|
+
});
|
|
129
|
+
for (const worker of page.workers())
|
|
130
|
+
void this.registerWorker(worker);
|
|
131
|
+
}
|
|
132
|
+
async registerContext(context, configuredHeaders = this.configuredHeaders) {
|
|
133
|
+
if (this.contexts.has(context))
|
|
134
|
+
return;
|
|
135
|
+
this.contexts.add(context);
|
|
136
|
+
this.contextConfiguredHeaders.set(context, configuredHeaders);
|
|
137
|
+
await context.addInitScript(({ attemptId, scopeHeader, scopeValue }) => {
|
|
138
|
+
globalThis.__SUPERCOV_MCDC_TEST_ID__ = attemptId;
|
|
139
|
+
try {
|
|
140
|
+
const originalFetch = globalThis.fetch?.bind(globalThis);
|
|
141
|
+
if (originalFetch) {
|
|
142
|
+
globalThis.fetch = ((input, init) => {
|
|
143
|
+
const headers = new Headers(init?.headers ??
|
|
144
|
+
(input instanceof Request ? input.headers : undefined));
|
|
145
|
+
headers.set(scopeHeader, scopeValue);
|
|
146
|
+
const phase = globalThis.__SUPERCOV_PHASE_ID__;
|
|
147
|
+
if (phase)
|
|
148
|
+
headers.set("x-supercov-phase", phase);
|
|
149
|
+
return originalFetch(input, { ...init, headers });
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
// Browser instrumentation must not change application behavior.
|
|
155
|
+
}
|
|
156
|
+
}, {
|
|
157
|
+
attemptId: this.scope.attemptId,
|
|
158
|
+
scopeHeader: COVERAGE_SCOPE_HEADER,
|
|
159
|
+
scopeValue: encodeCoverageScope(this.scope),
|
|
160
|
+
});
|
|
161
|
+
const attachPhase = async (route) => {
|
|
162
|
+
const phaseId = this.requestPhaseId();
|
|
163
|
+
await route.continue({
|
|
164
|
+
headers: {
|
|
165
|
+
...route.request().headers(),
|
|
166
|
+
[COVERAGE_SCOPE_HEADER]: encodeCoverageScope(this.scope),
|
|
167
|
+
...(phaseId ? { [COVERAGE_PHASE_HEADER]: phaseId } : {}),
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
};
|
|
171
|
+
this.contextRoutes.set(context, attachPhase);
|
|
172
|
+
await context.route("**/*", attachPhase);
|
|
173
|
+
const register = (page) => {
|
|
174
|
+
const pending = this.registerPage(page).finally(() => this.pendingRegistrations.delete(pending));
|
|
175
|
+
this.pendingRegistrations.add(pending);
|
|
176
|
+
};
|
|
177
|
+
context.on("page", register);
|
|
178
|
+
context.on("serviceworker", (worker) => {
|
|
179
|
+
void this.registerWorker(worker);
|
|
180
|
+
});
|
|
181
|
+
for (const worker of context.serviceWorkers())
|
|
182
|
+
void this.registerWorker(worker);
|
|
183
|
+
await this.updateContextHeaders(context, this.requestPhaseId());
|
|
184
|
+
for (const page of context.pages())
|
|
185
|
+
register(page);
|
|
186
|
+
}
|
|
187
|
+
async registerWorker(worker) {
|
|
188
|
+
if (this.workers.has(worker))
|
|
189
|
+
return;
|
|
190
|
+
this.workers.add(worker);
|
|
191
|
+
const phaseId = this.requestPhaseId();
|
|
192
|
+
await worker
|
|
193
|
+
.evaluate(({ attemptId, scopeHeader, scopeValue, phaseHeader, phase }) => {
|
|
194
|
+
globalThis.__SUPERCOV_MCDC_TEST_ID__ = attemptId;
|
|
195
|
+
if (phase)
|
|
196
|
+
globalThis.__SUPERCOV_PHASE_ID__ = phase;
|
|
197
|
+
const originalFetch = globalThis.fetch?.bind(globalThis);
|
|
198
|
+
if (!originalFetch)
|
|
199
|
+
return;
|
|
200
|
+
globalThis.fetch = ((input, init) => {
|
|
201
|
+
const headers = new Headers(init?.headers ??
|
|
202
|
+
(input instanceof Request ? input.headers : undefined));
|
|
203
|
+
headers.set(scopeHeader, scopeValue);
|
|
204
|
+
if (phase)
|
|
205
|
+
headers.set(phaseHeader, phase);
|
|
206
|
+
return originalFetch(input, { ...init, headers });
|
|
207
|
+
});
|
|
208
|
+
}, {
|
|
209
|
+
attemptId: this.scope.attemptId,
|
|
210
|
+
scopeHeader: COVERAGE_SCOPE_HEADER,
|
|
211
|
+
scopeValue: encodeCoverageScope(this.scope),
|
|
212
|
+
phaseHeader: COVERAGE_PHASE_HEADER,
|
|
213
|
+
phase: phaseId,
|
|
214
|
+
})
|
|
215
|
+
.catch(() => undefined);
|
|
216
|
+
}
|
|
217
|
+
async beginAction(operation) {
|
|
218
|
+
const phase = this.createPhase("action", operation);
|
|
219
|
+
this.lastActionId = phase.id;
|
|
220
|
+
this.activePhaseId = phase.id;
|
|
221
|
+
await this.activateInBrowser(phase.id);
|
|
222
|
+
return phase;
|
|
223
|
+
}
|
|
224
|
+
beginAssertion(operation) {
|
|
225
|
+
const phase = this.createPhase("assertion", operation, this.lastActionId);
|
|
226
|
+
this.activePhaseId = phase.id;
|
|
227
|
+
// Playwright queues browser protocol commands in order. Starting this
|
|
228
|
+
// evaluation before an async locator assertion is sufficient to tag its
|
|
229
|
+
// polling work without turning synchronous expect matchers into promises.
|
|
230
|
+
void this.activateInBrowser(phase.id);
|
|
231
|
+
return phase;
|
|
232
|
+
}
|
|
233
|
+
requestPhaseId() {
|
|
234
|
+
return this.activePhaseId;
|
|
235
|
+
}
|
|
236
|
+
async dispose() {
|
|
237
|
+
await Promise.all([...this.pendingRegistrations]);
|
|
238
|
+
await this.scriptUpdate;
|
|
239
|
+
for (const [page, cdp] of this.cdpSessions) {
|
|
240
|
+
const identifier = this.newDocumentScriptIds.get(page);
|
|
241
|
+
if (identifier)
|
|
242
|
+
await cdp
|
|
243
|
+
.send("Page.removeScriptToEvaluateOnNewDocument", {
|
|
244
|
+
identifier,
|
|
245
|
+
})
|
|
246
|
+
.catch(() => undefined);
|
|
247
|
+
await cdp.detach().catch(() => undefined);
|
|
248
|
+
}
|
|
249
|
+
for (const [context, route] of this.contextRoutes)
|
|
250
|
+
await context.unroute("**/*", route).catch(() => undefined);
|
|
251
|
+
}
|
|
252
|
+
finish(phase, error) {
|
|
253
|
+
phase.endedAtMs = Date.now();
|
|
254
|
+
phase.status = error === undefined ? "passed" : "failed";
|
|
255
|
+
if (error !== undefined)
|
|
256
|
+
phase.error = error instanceof Error ? error.message : String(error);
|
|
257
|
+
}
|
|
258
|
+
wrap(target) {
|
|
259
|
+
const cached = this.proxyCache.get(target);
|
|
260
|
+
if (cached)
|
|
261
|
+
return cached;
|
|
262
|
+
const proxy = new Proxy(target, {
|
|
263
|
+
get: (object, property, receiver) => {
|
|
264
|
+
if (property === "then")
|
|
265
|
+
return undefined;
|
|
266
|
+
// Playwright's locator matchers validate `receiver.constructor.name`.
|
|
267
|
+
// Preserve the native constructor rather than wrapping it as a method.
|
|
268
|
+
if (property === "constructor")
|
|
269
|
+
return object.constructor;
|
|
270
|
+
const value = Reflect.get(object, property, receiver);
|
|
271
|
+
if (typeof value !== "function")
|
|
272
|
+
return value;
|
|
273
|
+
const method = String(property);
|
|
274
|
+
return (...args) => {
|
|
275
|
+
const isRequest = REQUEST_METHODS.has(method) &&
|
|
276
|
+
isApiRequestContext(object);
|
|
277
|
+
if (ACTION_METHODS.has(method) || isRequest) {
|
|
278
|
+
return (async () => {
|
|
279
|
+
const operation = `${object.constructor?.name ?? "Playwright"}.${method}`;
|
|
280
|
+
const phase = await this.beginAction(operation);
|
|
281
|
+
try {
|
|
282
|
+
const result = await Reflect.apply(value, object, isRequest ? this.scopeApiRequest(args) : args);
|
|
283
|
+
this.finish(phase);
|
|
284
|
+
return this.prepareResult(result);
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
this.finish(phase, error);
|
|
288
|
+
throw error;
|
|
289
|
+
}
|
|
290
|
+
})();
|
|
291
|
+
}
|
|
292
|
+
const invokedArgs = method === "newContext" && object.constructor?.name === "Browser"
|
|
293
|
+
? this.scopeBrowserContext(args)
|
|
294
|
+
: args;
|
|
295
|
+
const result = Reflect.apply(value, object, invokedArgs);
|
|
296
|
+
return this.wrapResult(result, method === "newContext" ? invokedArgs : undefined);
|
|
297
|
+
};
|
|
298
|
+
},
|
|
299
|
+
});
|
|
300
|
+
this.proxyCache.set(target, proxy);
|
|
301
|
+
return proxy;
|
|
302
|
+
}
|
|
303
|
+
createPhase(kind, operation, causedByPhaseId) {
|
|
304
|
+
const source = callerSource();
|
|
305
|
+
const phase = {
|
|
306
|
+
id: `${this.scope.attemptId}:phase:${++this.counter}`,
|
|
307
|
+
kind,
|
|
308
|
+
operation,
|
|
309
|
+
...(source ? { source } : {}),
|
|
310
|
+
...(causedByPhaseId ? { causedByPhaseId } : {}),
|
|
311
|
+
startedAtMs: Date.now(),
|
|
312
|
+
};
|
|
313
|
+
this.phases.push(phase);
|
|
314
|
+
return phase;
|
|
315
|
+
}
|
|
316
|
+
wrapResult(result, sourceArgs) {
|
|
317
|
+
if (result instanceof Promise)
|
|
318
|
+
return result.then((resolved) => this.prepareResult(resolved, sourceArgs));
|
|
319
|
+
if (result === null ||
|
|
320
|
+
typeof result !== "object" ||
|
|
321
|
+
Array.isArray(result) ||
|
|
322
|
+
ArrayBuffer.isView(result))
|
|
323
|
+
return result;
|
|
324
|
+
return this.wrap(result);
|
|
325
|
+
}
|
|
326
|
+
async prepareResult(result, sourceArgs) {
|
|
327
|
+
if (!result ||
|
|
328
|
+
typeof result !== "object" ||
|
|
329
|
+
Array.isArray(result) ||
|
|
330
|
+
ArrayBuffer.isView(result))
|
|
331
|
+
return result;
|
|
332
|
+
const candidate = result;
|
|
333
|
+
if (typeof candidate["pages"] === "function" &&
|
|
334
|
+
typeof candidate["route"] === "function")
|
|
335
|
+
await this.registerContext(result, (sourceArgs?.[0]
|
|
336
|
+
?.extraHTTPHeaders ?? this.configuredHeaders));
|
|
337
|
+
if (typeof candidate["frames"] === "function" &&
|
|
338
|
+
typeof candidate["context"] === "function")
|
|
339
|
+
await this.registerPage(result);
|
|
340
|
+
return this.wrap(result);
|
|
341
|
+
}
|
|
342
|
+
scopeApiRequest(args) {
|
|
343
|
+
const scoped = [...args];
|
|
344
|
+
const optionIndex = 1;
|
|
345
|
+
const phaseId = this.requestPhaseId();
|
|
346
|
+
const coverageHeaders = {
|
|
347
|
+
[COVERAGE_SCOPE_HEADER]: encodeCoverageScope(this.scope),
|
|
348
|
+
...(phaseId ? { [COVERAGE_PHASE_HEADER]: phaseId } : {}),
|
|
349
|
+
};
|
|
350
|
+
const options = scoped[optionIndex] && typeof scoped[optionIndex] === "object"
|
|
351
|
+
? scoped[optionIndex]
|
|
352
|
+
: {};
|
|
353
|
+
scoped[optionIndex] = {
|
|
354
|
+
...options,
|
|
355
|
+
headers: mergeRequestHeaders(options.headers, coverageHeaders),
|
|
356
|
+
};
|
|
357
|
+
return scoped;
|
|
358
|
+
}
|
|
359
|
+
scopeBrowserContext(args) {
|
|
360
|
+
const scoped = [...args];
|
|
361
|
+
const options = scoped[0] && typeof scoped[0] === "object"
|
|
362
|
+
? scoped[0]
|
|
363
|
+
: {};
|
|
364
|
+
scoped[0] = {
|
|
365
|
+
...options,
|
|
366
|
+
extraHTTPHeaders: {
|
|
367
|
+
...(options.extraHTTPHeaders ?? {}),
|
|
368
|
+
...(activeCoverageHeaders() ?? {}),
|
|
369
|
+
},
|
|
370
|
+
};
|
|
371
|
+
return scoped;
|
|
372
|
+
}
|
|
373
|
+
async activateInBrowser(phaseId) {
|
|
374
|
+
await Promise.all([...this.contexts].map((context) => this.updateContextHeaders(context, phaseId)));
|
|
375
|
+
await Promise.all([...this.pages].flatMap((page) => page.frames()).map((frame) => frame
|
|
376
|
+
.evaluate(({ id, storageKey }) => {
|
|
377
|
+
globalThis.__SUPERCOV_PHASE_ID__ = id;
|
|
378
|
+
try {
|
|
379
|
+
localStorage.setItem(storageKey, id);
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
// Sandboxed/cross-origin frames may not expose localStorage.
|
|
383
|
+
}
|
|
384
|
+
}, { id: phaseId, storageKey: PHASE_STORAGE_KEY })
|
|
385
|
+
.catch(() => undefined)));
|
|
386
|
+
await Promise.all([...this.workers].map((worker) => worker
|
|
387
|
+
.evaluate((id) => {
|
|
388
|
+
globalThis.__SUPERCOV_PHASE_ID__ = id;
|
|
389
|
+
}, phaseId)
|
|
390
|
+
.catch(() => undefined)));
|
|
391
|
+
await Promise.all([...this.pages].map((page) => this.activatePage(page, phaseId)));
|
|
392
|
+
}
|
|
393
|
+
async updateContextHeaders(context, phaseId) {
|
|
394
|
+
await context
|
|
395
|
+
.setExtraHTTPHeaders({
|
|
396
|
+
...(this.contextConfiguredHeaders.get(context) ?? this.configuredHeaders),
|
|
397
|
+
[COVERAGE_SCOPE_HEADER]: encodeCoverageScope(this.scope),
|
|
398
|
+
...(phaseId ? { [COVERAGE_PHASE_HEADER]: phaseId } : {}),
|
|
399
|
+
})
|
|
400
|
+
.catch(() => undefined);
|
|
401
|
+
}
|
|
402
|
+
async activatePage(page, phaseId) {
|
|
403
|
+
const cdp = this.cdpSessions.get(page);
|
|
404
|
+
if (!cdp)
|
|
405
|
+
return;
|
|
406
|
+
this.scriptUpdate = this.scriptUpdate.then(async () => {
|
|
407
|
+
const previous = this.newDocumentScriptIds.get(page);
|
|
408
|
+
if (previous) {
|
|
409
|
+
await cdp.send("Page.removeScriptToEvaluateOnNewDocument", {
|
|
410
|
+
identifier: previous,
|
|
411
|
+
}).catch(() => undefined);
|
|
412
|
+
}
|
|
413
|
+
const installed = (await cdp
|
|
414
|
+
.send("Page.addScriptToEvaluateOnNewDocument", {
|
|
415
|
+
source: `globalThis.__SUPERCOV_PHASE_ID__=${JSON.stringify(phaseId)};`,
|
|
416
|
+
runImmediately: true,
|
|
417
|
+
})
|
|
418
|
+
.catch(() => undefined));
|
|
419
|
+
if (installed?.identifier)
|
|
420
|
+
this.newDocumentScriptIds.set(page, installed.identifier);
|
|
421
|
+
});
|
|
422
|
+
await this.scriptUpdate.catch(() => undefined);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
let activeController;
|
|
426
|
+
const controllers = new Map();
|
|
427
|
+
function activeCoverageHeaders() {
|
|
428
|
+
const controller = activeController;
|
|
429
|
+
if (!controller)
|
|
430
|
+
return undefined;
|
|
431
|
+
const phaseId = controller.requestPhaseId();
|
|
432
|
+
return {
|
|
433
|
+
[COVERAGE_SCOPE_HEADER]: encodeCoverageScope(controller.scope),
|
|
434
|
+
...(phaseId ? { [COVERAGE_PHASE_HEADER]: phaseId } : {}),
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
function mergeRequestHeaders(existing, coverage) {
|
|
438
|
+
if (existing instanceof Headers) {
|
|
439
|
+
return { ...Object.fromEntries(existing.entries()), ...coverage };
|
|
440
|
+
}
|
|
441
|
+
if (Array.isArray(existing)) {
|
|
442
|
+
const normalized = {};
|
|
443
|
+
for (let index = 0; index + 1 < existing.length; index += 2) {
|
|
444
|
+
normalized[String(existing[index])] = existing[index + 1];
|
|
445
|
+
}
|
|
446
|
+
return { ...normalized, ...coverage };
|
|
447
|
+
}
|
|
448
|
+
return {
|
|
449
|
+
...(existing && typeof existing === "object" ? existing : {}),
|
|
450
|
+
...coverage,
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
function scopedNodeRequestArguments(args) {
|
|
454
|
+
const coverage = activeCoverageHeaders();
|
|
455
|
+
if (!coverage || args.length === 0)
|
|
456
|
+
return args;
|
|
457
|
+
const scoped = [...args];
|
|
458
|
+
const first = scoped[0];
|
|
459
|
+
const startsWithUrl = typeof first === "string" || first instanceof URL;
|
|
460
|
+
const candidate = startsWithUrl ? scoped[1] : first;
|
|
461
|
+
const hasOptions = candidate !== null &&
|
|
462
|
+
typeof candidate === "object" &&
|
|
463
|
+
!(candidate instanceof URL);
|
|
464
|
+
if (hasOptions) {
|
|
465
|
+
const index = startsWithUrl ? 1 : 0;
|
|
466
|
+
const options = candidate;
|
|
467
|
+
scoped[index] = {
|
|
468
|
+
...options,
|
|
469
|
+
headers: mergeRequestHeaders(options.headers, coverage),
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
else if (startsWithUrl) {
|
|
473
|
+
scoped.splice(1, 0, { headers: coverage });
|
|
474
|
+
}
|
|
475
|
+
return scoped;
|
|
476
|
+
}
|
|
477
|
+
function installNodeRequestScopePropagation() {
|
|
478
|
+
const originalHttpRequest = http.request;
|
|
479
|
+
const originalHttpGet = http.get;
|
|
480
|
+
const originalHttpsRequest = https.request;
|
|
481
|
+
const originalHttpsGet = https.get;
|
|
482
|
+
http.request = ((...args) => Reflect.apply(originalHttpRequest, http, scopedNodeRequestArguments(args)));
|
|
483
|
+
http.get = ((...args) => Reflect.apply(originalHttpGet, http, scopedNodeRequestArguments(args)));
|
|
484
|
+
https.request = ((...args) => Reflect.apply(originalHttpsRequest, https, scopedNodeRequestArguments(args)));
|
|
485
|
+
https.get = ((...args) => Reflect.apply(originalHttpsGet, https, scopedNodeRequestArguments(args)));
|
|
486
|
+
// Existing ESM named imports such as `import { request } from "node:http"`
|
|
487
|
+
// must observe the patched CommonJS-compatible builtin exports too.
|
|
488
|
+
syncBuiltinESMExports();
|
|
489
|
+
if (typeof globalThis.fetch === "function") {
|
|
490
|
+
const originalFetch = globalThis.fetch.bind(globalThis);
|
|
491
|
+
globalThis.fetch = ((input, init) => {
|
|
492
|
+
const coverage = activeCoverageHeaders();
|
|
493
|
+
if (!coverage)
|
|
494
|
+
return originalFetch(input, init);
|
|
495
|
+
const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined));
|
|
496
|
+
for (const [name, value] of Object.entries(coverage))
|
|
497
|
+
headers.set(name, value);
|
|
498
|
+
return originalFetch(input, { ...init, headers });
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
installNodeRequestScopePropagation();
|
|
503
|
+
function scopedChildOptions(args, optionIndex) {
|
|
504
|
+
const controller = activeController;
|
|
505
|
+
if (!controller)
|
|
506
|
+
return args;
|
|
507
|
+
const scoped = [...args];
|
|
508
|
+
const existing = scoped[optionIndex] && typeof scoped[optionIndex] === "object"
|
|
509
|
+
? scoped[optionIndex]
|
|
510
|
+
: {};
|
|
511
|
+
const options = {
|
|
512
|
+
...existing,
|
|
513
|
+
env: {
|
|
514
|
+
...process.env,
|
|
515
|
+
...(existing.env ?? {}),
|
|
516
|
+
SUPERCOV_RUN_ID: controller.scope.runId,
|
|
517
|
+
[COVERAGE_CARRIER_ENV]: encodeCoverageCarrier({
|
|
518
|
+
version: 1,
|
|
519
|
+
scope: controller.scope,
|
|
520
|
+
...(controller.requestPhaseId()
|
|
521
|
+
? { phaseId: controller.requestPhaseId() }
|
|
522
|
+
: {}),
|
|
523
|
+
}),
|
|
524
|
+
},
|
|
525
|
+
};
|
|
526
|
+
if (typeof scoped[optionIndex] === "function")
|
|
527
|
+
scoped.splice(optionIndex, 0, options);
|
|
528
|
+
else
|
|
529
|
+
scoped[optionIndex] = options;
|
|
530
|
+
return scoped;
|
|
531
|
+
}
|
|
532
|
+
function childOptionIndex(method, args) {
|
|
533
|
+
if (method === "spawn" || method === "spawnSync" || method === "fork")
|
|
534
|
+
return Array.isArray(args[1]) ? 2 : 1;
|
|
535
|
+
if (method === "execFile" || method === "execFileSync")
|
|
536
|
+
return Array.isArray(args[1]) ? 2 : 1;
|
|
537
|
+
return 1;
|
|
538
|
+
}
|
|
539
|
+
function installChildProcessScopePropagation() {
|
|
540
|
+
for (const method of [
|
|
541
|
+
"exec",
|
|
542
|
+
"execFile",
|
|
543
|
+
"execFileSync",
|
|
544
|
+
"execSync",
|
|
545
|
+
"fork",
|
|
546
|
+
"spawn",
|
|
547
|
+
"spawnSync",
|
|
548
|
+
]) {
|
|
549
|
+
const original = childProcess[method];
|
|
550
|
+
childProcess[method] = function (...args) {
|
|
551
|
+
return Reflect.apply(original, childProcess, scopedChildOptions(args, childOptionIndex(method, args)));
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
syncBuiltinESMExports();
|
|
555
|
+
}
|
|
556
|
+
installChildProcessScopePropagation();
|
|
557
|
+
function wrapMatchers(matchers, path = "expect") {
|
|
558
|
+
return new Proxy(matchers, {
|
|
559
|
+
get(target, property, receiver) {
|
|
560
|
+
const value = Reflect.get(target, property, receiver);
|
|
561
|
+
const name = String(property);
|
|
562
|
+
if (value && typeof value === "object")
|
|
563
|
+
return wrapMatchers(value, `${path}.${name}`);
|
|
564
|
+
if (typeof value !== "function")
|
|
565
|
+
return value;
|
|
566
|
+
return (...args) => {
|
|
567
|
+
const controller = activeController;
|
|
568
|
+
const phase = controller?.beginAssertion(`${path}.${name}`);
|
|
569
|
+
try {
|
|
570
|
+
const result = Reflect.apply(value, target, args);
|
|
571
|
+
if (result instanceof Promise) {
|
|
572
|
+
return result.then((resolved) => {
|
|
573
|
+
if (phase && controller)
|
|
574
|
+
controller.finish(phase);
|
|
575
|
+
return resolved;
|
|
576
|
+
}, (error) => {
|
|
577
|
+
if (phase && controller)
|
|
578
|
+
controller.finish(phase, error);
|
|
579
|
+
throw error;
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
if (phase && controller)
|
|
583
|
+
controller.finish(phase);
|
|
584
|
+
return result;
|
|
585
|
+
}
|
|
586
|
+
catch (error) {
|
|
587
|
+
if (phase && controller)
|
|
588
|
+
controller.finish(phase, error);
|
|
589
|
+
throw error;
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
},
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
function wrapExpectCallable(callable) {
|
|
596
|
+
return new Proxy(callable, {
|
|
597
|
+
apply(target, thisArg, argumentsList) {
|
|
598
|
+
const result = Reflect.apply(target, thisArg, argumentsList);
|
|
599
|
+
if (typeof result === "function")
|
|
600
|
+
return wrapExpectCallable(result);
|
|
601
|
+
if (result && typeof result === "object")
|
|
602
|
+
return wrapMatchers(result);
|
|
603
|
+
return result;
|
|
604
|
+
},
|
|
605
|
+
get(target, property, receiver) {
|
|
606
|
+
const value = Reflect.get(target, property, receiver);
|
|
607
|
+
if (typeof value !== "function")
|
|
608
|
+
return value;
|
|
609
|
+
return wrapExpectCallable(value.bind(target));
|
|
610
|
+
},
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
export const expect = wrapExpectCallable(baseExpect);
|
|
614
|
+
function currentRunId() {
|
|
615
|
+
return (process.env["SUPERCOV_RUN_ID"] ??
|
|
616
|
+
(GENERATED_RUN_ID.startsWith("__") ? "unscoped" : GENERATED_RUN_ID));
|
|
617
|
+
}
|
|
618
|
+
function executionScope(testInfo) {
|
|
619
|
+
const runId = currentRunId();
|
|
620
|
+
const workerId = `pid-${process.pid}-worker-${testInfo.workerIndex}`;
|
|
621
|
+
const testKey = createHash("sha256")
|
|
622
|
+
.update(testInfo.testId)
|
|
623
|
+
.digest("hex")
|
|
624
|
+
.slice(0, 24);
|
|
625
|
+
const attemptId = createHash("sha256")
|
|
626
|
+
.update(`${runId}\0${workerId}\0${testInfo.testId}\0${testInfo.retry}`)
|
|
627
|
+
.digest("hex")
|
|
628
|
+
.slice(0, 24);
|
|
629
|
+
return {
|
|
630
|
+
version: 1,
|
|
631
|
+
runId,
|
|
632
|
+
workerId,
|
|
633
|
+
testId: testInfo.testId,
|
|
634
|
+
testKey,
|
|
635
|
+
retry: testInfo.retry,
|
|
636
|
+
attemptId,
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
function readServerRecords(scope) {
|
|
640
|
+
try {
|
|
641
|
+
return readFileSync(serverEvidencePath(scope), "utf8")
|
|
642
|
+
.split("\n")
|
|
643
|
+
.filter(Boolean)
|
|
644
|
+
.flatMap((line) => {
|
|
645
|
+
try {
|
|
646
|
+
const record = JSON.parse(line);
|
|
647
|
+
return record.scope?.attemptId === scope.attemptId ? [record] : [];
|
|
648
|
+
}
|
|
649
|
+
catch {
|
|
650
|
+
// Ignore a final partial line if the server was writing during collection.
|
|
651
|
+
return [];
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
catch {
|
|
656
|
+
return [];
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
const instrumentedTest = base.extend({
|
|
660
|
+
page: async ({ page }, use, testInfo) => {
|
|
661
|
+
const scope = executionScope(testInfo);
|
|
662
|
+
const configuredHeaders = Object.fromEntries(Object.entries(testInfo.project.use.extraHTTPHeaders ?? {}).map(([name, value]) => [name, String(value)]));
|
|
663
|
+
const controller = new CoveragePhaseController(scope, configuredHeaders);
|
|
664
|
+
controllers.set(scope.attemptId, controller);
|
|
665
|
+
activeController = controller;
|
|
666
|
+
await controller.registerPage(page);
|
|
667
|
+
try {
|
|
668
|
+
await use(controller.wrap(page));
|
|
669
|
+
}
|
|
670
|
+
finally {
|
|
671
|
+
await controller.dispose();
|
|
672
|
+
if (activeController === controller)
|
|
673
|
+
activeController = undefined;
|
|
674
|
+
controllers.delete(scope.attemptId);
|
|
675
|
+
}
|
|
676
|
+
},
|
|
677
|
+
browser: [
|
|
678
|
+
async ({ browser }, use) => {
|
|
679
|
+
await use(new Proxy(browser, {
|
|
680
|
+
get(target, property, receiver) {
|
|
681
|
+
const controller = activeController;
|
|
682
|
+
return controller
|
|
683
|
+
? Reflect.get(controller.wrap(target), property, receiver)
|
|
684
|
+
: Reflect.get(target, property, receiver);
|
|
685
|
+
},
|
|
686
|
+
}));
|
|
687
|
+
},
|
|
688
|
+
{ scope: "worker" },
|
|
689
|
+
],
|
|
690
|
+
request: async ({ request }, use) => {
|
|
691
|
+
await use(new Proxy(request, {
|
|
692
|
+
get(target, property, receiver) {
|
|
693
|
+
const controller = activeController;
|
|
694
|
+
return controller
|
|
695
|
+
? Reflect.get(controller.wrap(target), property, receiver)
|
|
696
|
+
: Reflect.get(target, property, receiver);
|
|
697
|
+
},
|
|
698
|
+
}));
|
|
699
|
+
},
|
|
700
|
+
mcdcAutoCollect: [
|
|
701
|
+
async ({ page }, use, testInfo) => {
|
|
702
|
+
const scope = executionScope(testInfo);
|
|
703
|
+
const serverOutput = serverEvidencePath(scope);
|
|
704
|
+
mkdirSync(serverEvidenceDirectory(scope), { recursive: true });
|
|
705
|
+
rmSync(serverOutput, { force: true });
|
|
706
|
+
try {
|
|
707
|
+
await use();
|
|
708
|
+
}
|
|
709
|
+
finally {
|
|
710
|
+
const controller = controllers.get(scope.attemptId);
|
|
711
|
+
const browser = [];
|
|
712
|
+
const pages = controller?.allPages() ?? [page];
|
|
713
|
+
for (const currentPage of pages) {
|
|
714
|
+
for (const frame of currentPage.frames()) {
|
|
715
|
+
const frameSnapshot = await frame
|
|
716
|
+
.evaluate(() => {
|
|
717
|
+
const getSnapshot = globalThis.__SUPERCOV_COVERAGE_SNAPSHOT__;
|
|
718
|
+
return getSnapshot?.() ?? { decisions: [], hits: [], events: [] };
|
|
719
|
+
})
|
|
720
|
+
.catch(() => ({
|
|
721
|
+
decisions: [],
|
|
722
|
+
hits: [],
|
|
723
|
+
events: [],
|
|
724
|
+
}));
|
|
725
|
+
browser.push(frameSnapshot);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
for (const worker of controller?.allWorkers() ?? []) {
|
|
729
|
+
const workerSnapshot = await worker
|
|
730
|
+
.evaluate(() => {
|
|
731
|
+
const getSnapshot = globalThis.__SUPERCOV_COVERAGE_SNAPSHOT__;
|
|
732
|
+
return getSnapshot?.() ?? { decisions: [], hits: [], events: [] };
|
|
733
|
+
})
|
|
734
|
+
.catch(() => ({
|
|
735
|
+
decisions: [],
|
|
736
|
+
hits: [],
|
|
737
|
+
events: [],
|
|
738
|
+
}));
|
|
739
|
+
browser.push(workerSnapshot);
|
|
740
|
+
}
|
|
741
|
+
const server = readServerRecords(scope);
|
|
742
|
+
// Emit an artifact even when this test touched no application source.
|
|
743
|
+
// A complete test-to-coverage matrix must also identify tests that are
|
|
744
|
+
// removable without changing coverage.
|
|
745
|
+
const outputPath = testInfo.outputPath("mcdc.json");
|
|
746
|
+
mkdirSync(dirname(outputPath), { recursive: true });
|
|
747
|
+
const testFile = relative(process.cwd(), testInfo.file)
|
|
748
|
+
.split(sep)
|
|
749
|
+
.join("/");
|
|
750
|
+
const payload = {
|
|
751
|
+
testId: testInfo.testId,
|
|
752
|
+
scope,
|
|
753
|
+
test: testInfo.titlePath.join(" > "),
|
|
754
|
+
testFile,
|
|
755
|
+
title: testInfo.title,
|
|
756
|
+
retry: testInfo.retry,
|
|
757
|
+
status: testInfo.status ?? "unknown",
|
|
758
|
+
expectedStatus: testInfo.expectedStatus,
|
|
759
|
+
provenance: inferTestProvenance({
|
|
760
|
+
runner: "playwright",
|
|
761
|
+
file: testFile,
|
|
762
|
+
project: testInfo.project.name,
|
|
763
|
+
explicitKind: process.env["SUPERCOV_TEST_KIND"],
|
|
764
|
+
}),
|
|
765
|
+
phases: controller?.phases ?? [],
|
|
766
|
+
browser,
|
|
767
|
+
server,
|
|
768
|
+
};
|
|
769
|
+
const serialized = `${JSON.stringify(payload)}\n`;
|
|
770
|
+
writeFileSync(outputPath, serialized);
|
|
771
|
+
// Pool runners may cycle-restore a VM immediately after Playwright
|
|
772
|
+
// exits, which can discard or overwrite the normal artifact copy.
|
|
773
|
+
// Write one uniquely named, one-shot evidence file to the runner's
|
|
774
|
+
// shared directory as well. No test streams into this path.
|
|
775
|
+
const evidenceDirectory = process.env["SUPERCOV_EVIDENCE_DIR"] ??
|
|
776
|
+
(GENERATED_EVIDENCE_DIRECTORY.startsWith("__")
|
|
777
|
+
? undefined
|
|
778
|
+
: GENERATED_EVIDENCE_DIRECTORY);
|
|
779
|
+
if (evidenceDirectory) {
|
|
780
|
+
const resolvedDirectory = resolve(process.cwd(), evidenceDirectory);
|
|
781
|
+
const safeTestId = testInfo.testId.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
782
|
+
const testEvidenceDirectory = resolve(resolvedDirectory, `${safeTestId}-${testInfo.retry}`);
|
|
783
|
+
mkdirSync(testEvidenceDirectory, { recursive: true });
|
|
784
|
+
writeFileSync(resolve(testEvidenceDirectory, "mcdc.json"), serialized);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
},
|
|
788
|
+
{ auto: true },
|
|
789
|
+
],
|
|
790
|
+
});
|
|
791
|
+
export const test = instrumentedTest;
|
|
792
|
+
export const offlineTest = instrumentedTest;
|
|
793
|
+
export const WebhookBodiesContract = adapter.WebhookBodiesContract;
|
|
794
|
+
//# sourceMappingURL=playwright.js.map
|