supercov 4.27.0 → 4.29.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 (32) hide show
  1. package/README.md +43 -16
  2. package/docs/cli.md +358 -9
  3. package/docs/getting-started.md +18 -0
  4. package/docs/mutation.md +539 -0
  5. package/docs/supported-suites.md +109 -60
  6. package/package.json +11 -10
  7. package/runtime/javascript/atomic.mjs +6 -2
  8. package/runtime/javascript/ava.mjs +12 -0
  9. package/runtime/javascript/bunTest.mjs +17 -0
  10. package/runtime/javascript/jest.cjs +67 -0
  11. package/runtime/javascript/jest.config.mjs +46 -6
  12. package/runtime/javascript/jestReporter.mjs +5 -2
  13. package/runtime/javascript/mocha.mjs +79 -5
  14. package/runtime/javascript/nodeTest.mjs +28 -0
  15. package/runtime/javascript/playwright.mjs +33 -0
  16. package/runtime/javascript/playwrightStrong.mjs +477 -0
  17. package/runtime/javascript/playwrightStrongReporter.mjs +377 -0
  18. package/runtime/javascript/runnerEntry.mjs +62 -0
  19. package/runtime/javascript/runtime.mjs +752 -13
  20. package/runtime/javascript/snapshots.mjs +16 -1
  21. package/runtime/javascript/strongJestHost.mjs +103 -0
  22. package/runtime/javascript/strongJob.mjs +78 -0
  23. package/runtime/javascript/strongJobs.mjs +809 -0
  24. package/runtime/javascript/strongMochaHost.mjs +166 -0
  25. package/runtime/javascript/strongWasmOut.cjs +96 -0
  26. package/runtime/javascript/tap.mjs +43 -1
  27. package/runtime/javascript/transport.mjs +5 -0
  28. package/runtime/javascript/vitest.mjs +74 -2
  29. package/runtime/javascript/vitestBrowser.mjs +34 -2
  30. package/runtime/javascript/vitestStrong.mjs +234 -0
  31. package/runtime/javascript/vitestStrongReporter.mjs +2268 -0
  32. package/runtime/javascript/vitestStrongSpawner.mjs +248 -0
@@ -12,7 +12,8 @@ import { join, relative, resolve, sep } from "node:path";
12
12
  import { pathToFileURL } from "node:url";
13
13
  import { appendEvidenceRecord } from "./atomic.mjs";
14
14
  import { inferTestProvenance, testTags } from "./provenance.mjs";
15
- import { runnerExecutionScope } from "./runnerEvidence.mjs";
15
+ import { runnerExecutionScope, runnerTestId } from "./runnerEvidence.mjs";
16
+ import { strongJob } from "./strongJob.mjs";
16
17
 
17
18
  const SETUP_HOOK = /^"(?:before|after) all" hook/;
18
19
 
@@ -55,15 +56,29 @@ export async function adaptMocha(entrypoint) {
55
56
  // One snapshot per test, as under Jest; the server transport would only
56
57
  // repeat it, unattributed.
57
58
  runtime.enableRuntimeSnapshotEvidence();
58
- const directory = resolve(process.cwd(), evidenceDirectory);
59
- const write = (payload) => appendEvidenceRecord(directory, "mocha", payload);
59
+ // Read as each record is written: a resident Mocha (strongMochaHost.mjs)
60
+ // writes each job's into the job's own directory.
61
+ const write = (payload) => appendEvidenceRecord(resolve(process.cwd(), process.env.SUPERCOV_EVIDENCE_DIR ?? evidenceDirectory), "mocha", payload);
60
62
  // A --parallel parent overrides run() and never reaches this: its workers
61
63
  // run the tests, each with a Runner of its own.
62
64
  Runner.prototype.run = function supercovMochaRun(...args) {
65
+ const job = strongJob();
63
66
  observe(this, runtime, write);
67
+ if (job) {
68
+ keepJobTests(this.suite, job);
69
+ // A failure settles the job's mutant: the rest need not run.
70
+ this.on("fail", (runnable) => {
71
+ job.fail();
72
+ if (job.stopped && typeof this.abort === "function")
73
+ this.abort();
74
+ });
75
+ }
64
76
  return Reflect.apply(run, this, args);
65
77
  };
66
78
  Runner.prototype.run.supercovMocha = true;
79
+ // A strong pass's job loads only its own test file, of the run's.
80
+ if (strongJob())
81
+ await keepJobFiles(root);
67
82
  // Mocha takes its working directory off the front of each frame and
68
83
  // prints the rest. Under Supercov that is the copy, while an application
69
84
  // frame names the project's own file, so it printed whole:
@@ -81,6 +96,59 @@ export async function adaptMocha(entrypoint) {
81
96
  }
82
97
  }
83
98
 
99
+ // Mocha's test ids, as `observe` gives them, in the order Mocha runs the
100
+ // tests (a suite's own tests, then its suites'); a strong pass's job removes
101
+ // every test it does not run, and a suite left with none runs no hook.
102
+ function keepJobTests(root, job) {
103
+ const occurrences = new Map();
104
+ const visit = (suite) => {
105
+ suite.tests = suite.tests.filter((test) => {
106
+ const name = test.titlePath().join(" > ");
107
+ const key = `${test.file ?? ""}\0${name}`;
108
+ const occurrence = occurrences.get(key) ?? 0;
109
+ occurrences.set(key, occurrence + 1);
110
+ test.supercovOccurrence = occurrence;
111
+ const testId = runnerTestId({
112
+ runner: "mocha",
113
+ file: test.file,
114
+ name,
115
+ ...(occurrence ? { registrationOrdinal: occurrence } : {}),
116
+ });
117
+ return job.selects(test.file, testId);
118
+ });
119
+ suite.suites.forEach(visit);
120
+ };
121
+ visit(root);
122
+ }
123
+
124
+ // Leaves out the run's other test files before Mocha loads them; under
125
+ // --parallel, before it hands them to its workers.
126
+ async function keepJobFiles(root) {
127
+ let loaded;
128
+ try {
129
+ loaded = await import(pathToFileURL(join(root, "lib", "mocha.js")).href);
130
+ }
131
+ catch {
132
+ return;
133
+ }
134
+ const Mocha = loaded.Mocha ?? loaded.default?.Mocha ?? loaded.default;
135
+ const prototype = Mocha?.prototype;
136
+ if (!prototype || prototype.supercovJobFiles)
137
+ return;
138
+ prototype.supercovJobFiles = true;
139
+ for (const name of ["loadFiles", "loadFilesAsync", "run"]) {
140
+ const original = prototype[name];
141
+ if (typeof original !== "function")
142
+ continue;
143
+ prototype[name] = function supercovJobFiles(...args) {
144
+ const job = strongJob();
145
+ if (job && Array.isArray(this.files))
146
+ this.files = this.files.filter((file) => job.runsFile(file));
147
+ return Reflect.apply(original, this, args);
148
+ };
149
+ }
150
+ }
151
+
84
152
  const HOOK_FRAME = /^\s*at process\.(?:callbackTrampoline|topLevelDomainCallback) \(node:(?:internal\/async_hooks|domain):\d+:\d+\)$/;
85
153
 
86
154
  function asWritten() {
@@ -131,6 +199,9 @@ function observe(runner, runtime, write) {
131
199
  occurrence = Math.max(occurrence - 1, 0);
132
200
  else
133
201
  firstAttempts.set(key, occurrence + 1);
202
+ // A strong pass's job numbered its tests before it left some out.
203
+ if (test.supercovOccurrence !== undefined)
204
+ occurrence = test.supercovOccurrence;
134
205
  const value = {
135
206
  runner: "mocha",
136
207
  file,
@@ -141,7 +212,7 @@ function observe(runner, runtime, write) {
141
212
  identities.set(test, value);
142
213
  return value;
143
214
  }
144
- function record(id, status, scope, snapshot) {
215
+ function record(id, status, scope, snapshot, durationMs) {
145
216
  const testFile = localFile(id.file);
146
217
  write({
147
218
  testId: scope ? scope.testId : runnerExecutionScope(id).testId,
@@ -151,6 +222,9 @@ function observe(runner, runtime, write) {
151
222
  title: id.name.split(" > ").at(-1) ?? id.name,
152
223
  retry: id.retry ?? 0,
153
224
  status,
225
+ // How long the test took: a strong pass runs a mutant's cheapest
226
+ // test files first, and gives each its limit.
227
+ ...(typeof durationMs === "number" ? { durationMs: Math.round(durationMs) } : {}),
154
228
  ...(id.role ? { role: id.role } : {}),
155
229
  provenance: inferTestProvenance({
156
230
  runner: "mocha",
@@ -179,7 +253,7 @@ function observe(runner, runtime, write) {
179
253
  active = undefined;
180
254
  if (!current)
181
255
  return;
182
- record(current.id, current.status, current.scope, runtime.coverageSnapshot());
256
+ record(current.id, current.status, current.scope, runtime.coverageSnapshot(), current.test.duration);
183
257
  runtime.resetCoverage();
184
258
  runtime.activateCoverageScope();
185
259
  }
@@ -4,6 +4,7 @@ import { fileURLToPath } from "node:url";
4
4
  import vm from "node:vm";
5
5
  import { beginBufferedServerEvidence, flushBufferedServerEvidence, takeNodeAssertionPhases, withCoverageCarrier, withStackRoom, } from "./runtime.mjs";
6
6
  import { callerLocation, runnerExecutionScope, runnerTestId, stackAsWritten, writeRunnerEvidence, } from "./runnerEvidence.mjs";
7
+ import { strongJob } from "./strongJob.mjs";
7
8
  function callbackIndex(args) {
8
9
  for (let index = args.length - 1; index >= 0; index -= 1)
9
10
  if (typeof args[index] === "function")
@@ -131,6 +132,9 @@ function wrappedSuite(original, mark) {
131
132
  }
132
133
  return wrapped;
133
134
  }
135
+ function emitSkipped(identity, scope, evidenceDirectory) {
136
+ writeRunnerEvidence(identity, "skipped", scope, evidenceDirectory);
137
+ }
134
138
  const registrationCounts = new Map();
135
139
  function wrappedRegistration(original, parentTestId, forcedStatus) {
136
140
  const wrapped = function supercovNodeTest(...args) {
@@ -155,6 +159,10 @@ function wrappedRegistration(original, parentTestId, forcedStatus) {
155
159
  };
156
160
  // Allocate at registration, never callback completion: concurrent tests
157
161
  // can finish in any order. Count each source/name within its parent.
162
+ if (!parentTestId) {
163
+ const counted = Symbol.for("supercov.nodeTestRegistrations");
164
+ globalThis[counted] = (globalThis[counted] ?? 0) + 1;
165
+ }
158
166
  const registrationKey = runnerTestId(identity);
159
167
  const registrationOrdinal = registrationCounts.get(registrationKey) ?? 0;
160
168
  registrationCounts.set(registrationKey, registrationOrdinal + 1);
@@ -167,6 +175,19 @@ function wrappedRegistration(original, parentTestId, forcedStatus) {
167
175
  writeRunnerEvidence(identity, marked ?? "passed", scope, evidenceDirectory);
168
176
  return Reflect.apply(original, this, args);
169
177
  }
178
+ // A strong pass's job runs its own tests; a subtest runs with its
179
+ // parent, as it does without Supercov.
180
+ const job = strongJob();
181
+ if (job && !parentTestId && !job.selects(identity.file, scope.testId)) {
182
+ const skipped = [...args];
183
+ skipped[index] = () => { };
184
+ const at = skipped.findIndex((value, place) => place < index && value === options);
185
+ if (at >= 0)
186
+ skipped[at] = { ...options, skip: true };
187
+ else
188
+ skipped.splice(index, 0, { skip: true });
189
+ return Reflect.apply(original, this, skipped);
190
+ }
170
191
  if (marked)
171
192
  writeRunnerEvidence(identity, marked, scope, evidenceDirectory);
172
193
  const next = [...args];
@@ -177,10 +198,17 @@ function wrappedRegistration(original, parentTestId, forcedStatus) {
177
198
  beginBufferedServerEvidence(scope);
178
199
  // A todo test runs, and stays todo whether its body passes or fails.
179
200
  let status = marked ?? "passed";
201
+ const job = strongJob();
180
202
  const fail = () => {
181
203
  if (status !== "todo")
182
204
  status = "failed";
205
+ job?.fail();
183
206
  };
207
+ // A strong pass's job stops at its first failure.
208
+ if (job?.stopped && !marked) {
209
+ emitSkipped(identity, scope, evidenceDirectory);
210
+ return context.skip();
211
+ }
184
212
  const contextProxy = new Proxy(context, {
185
213
  get(target, property) {
186
214
  // Read with the real context as receiver: TestContext accessors
@@ -20,6 +20,7 @@ import { inferTestProvenance, testTags } from "./provenance.mjs";
20
20
  import { appendJsonLineDurableSync, appendJsonLineSync } from "./atomic.mjs";
21
21
  import { expandSnapshot, mergeSnapshots, columnarEvents } from "./snapshots.mjs";
22
22
  import { COVERAGE_DOCUMENT_PHASE_COOKIE, COVERAGE_DOCUMENT_PHASE_SITE_COOKIE, COVERAGE_PHASE_HEADER, COVERAGE_PHASE_COOKIE, COVERAGE_PHASE_SITE_COOKIE, COVERAGE_SCOPE_COOKIE, COVERAGE_SCOPE_HEADER, COVERAGE_SCOPE_SITE_COOKIE, COVERAGE_CARRIER_ENV, encodeCoverageCarrier, encodeCoverageScope, serverEvidenceDirectory, serverEvidencePath, startDocumentInPhase, } from "./transport.mjs";
23
+ import { PAGE_TIMEOUT_MARKER, activatePageMutant, strongClaim, strongFinish } from "./playwrightStrong.mjs";
23
24
  export * from "@playwright/test";
24
25
  const generatedTargetModule = "__SUPERCOV_PLAYWRIGHT_MODULE__";
25
26
  const targetModule = process.env["SUPERCOV_PLAYWRIGHT_MODULE"] ??
@@ -557,6 +558,13 @@ class CoveragePhaseController {
557
558
  page.on("worker", (worker) => {
558
559
  void this.registerWorker(worker);
559
560
  });
561
+ // A strong pass's page whose mutant ran past its hit limit says so.
562
+ if (this.scope.mutant && this.scope.mutant !== "-") {
563
+ page.on("console", (message) => {
564
+ if (message.text().includes(PAGE_TIMEOUT_MARKER))
565
+ this.mutantRanAway = true;
566
+ });
567
+ }
560
568
  for (const worker of page.workers())
561
569
  void this.registerWorker(worker);
562
570
  }
@@ -593,6 +601,15 @@ class CoveragePhaseController {
593
601
  // removed once this one is in place, so a document runs one copy:
594
602
  // they accumulated to some fifty per context on a long suite, each
595
603
  // sent to every new page and frame and run by every document.
604
+ // A strong pass's attempt: its mutant on in every document, before
605
+ // the page's own scripts.
606
+ const mutant = this.scope.mutant;
607
+ if (mutant) {
608
+ const at = mutant.lastIndexOf("#");
609
+ parts.push(`(${activatePageMutant})(${JSON.stringify(mutant === "-"
610
+ ? { file: null, index: -1, marker: PAGE_TIMEOUT_MARKER }
611
+ : { file: mutant.slice(0, at), index: Number(mutant.slice(at + 1)), marker: PAGE_TIMEOUT_MARKER })});`);
612
+ }
596
613
  parts.push(`(${announceAttempt})(${JSON.stringify({
597
614
  attemptId: this.scope.attemptId,
598
615
  scopeHeader: COVERAGE_SCOPE_HEADER,
@@ -1198,6 +1215,10 @@ function prepareContext(context) {
1198
1215
  // their wasm instances, so each test's work can be taken from them.
1199
1216
  if (process.env.SUPERCOV_WASM_REGIONS === "1")
1200
1217
  parts.push("globalThis.__SUPERCOV_WASM_REGIONS__ = true;");
1218
+ // A strong pass over such a crate: pages hand each wasm instance the
1219
+ // attempt's mutant, by its file in the crate's Cargo workspace.
1220
+ if (process.env.SUPERCOV_WASM_STRONG)
1221
+ parts.push(`Object.defineProperty(globalThis, "__SUPERCOV_WASM_STRONG__", { value: ${JSON.stringify(JSON.parse(process.env.SUPERCOV_WASM_STRONG))}, writable: true, configurable: true });`);
1201
1222
  parts.push(browserRuntimePrelude);
1202
1223
  prepared = Promise.resolve(internally(context, () => Promise.all([
1203
1224
  context.addInitScript({ content: separately(parts) }).catch(() => undefined),
@@ -1735,8 +1756,13 @@ function executionScope(testInfo) {
1735
1756
  testKey,
1736
1757
  retry: testInfo.retry,
1737
1758
  attemptId,
1759
+ // A strong pass's attempt names its mutant; the scope carries it to
1760
+ // the app's server with every request.
1761
+ ...(strongMutants.has(testInfo) ? { mutant: strongMutants.get(testInfo) } : {}),
1738
1762
  };
1739
1763
  }
1764
+ // Each strong attempt's mutant (`file#index`, `-` for none), by its TestInfo.
1765
+ const strongMutants = new WeakMap();
1740
1766
  function phaseSourceLine(source) {
1741
1767
  return source?.replace(/:\d+$/, "");
1742
1768
  }
@@ -1834,6 +1860,12 @@ const instrumentedFixtures = {
1834
1860
  },
1835
1861
  mcdcAutoCollect: [
1836
1862
  async ({}, use, testInfo) => {
1863
+ // A strong pass: what this repetition runs, or a skip.
1864
+ const strong = await strongClaim(testInfo);
1865
+ if (strong === null)
1866
+ testInfo.skip(true, "Supercov strong mutation: nothing for this repetition");
1867
+ if (strong)
1868
+ strongMutants.set(testInfo, strong.mutant);
1837
1869
  const scope = executionScope(testInfo);
1838
1870
  const configuredHeaders = Object.fromEntries(Object.entries(testInfo.project.use.extraHTTPHeaders ?? {}).map(([name, value]) => [name, String(value)]));
1839
1871
  const setupStarted = performance.now();
@@ -1911,6 +1943,7 @@ const instrumentedFixtures = {
1911
1943
  appendEvidenceLine(`${JSON.stringify(payload)}\n`);
1912
1944
  }
1913
1945
  finally {
1946
+ strongFinish(testInfo, strong, { attemptId: scope.attemptId, pageTimeout: controller.mutantRanAway === true });
1914
1947
  await timed("controller.dispose", () => controller.dispose());
1915
1948
  timingCount("fixture.testTeardown", performance.now() - teardownStarted);
1916
1949
  timingReport(testInfo.title);