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
@@ -0,0 +1,248 @@
1
+ // Strong mutation's process spawner (docs/design/strong-mutation.md).
2
+ //
3
+ // A strong pass starts a worker process for every copy of a test file, and
4
+ // Vitest forks each from its own process. Forking copies the parent's memory
5
+ // map, and Vitest's process holds about a gigabyte by then (its module graph,
6
+ // the bundler's native heap): on hono each fork held Vitest's event loop for
7
+ // 24 ms, and the workers then waited on it. This process starts small and
8
+ // stays small, forks the workers in Vitest's stead, and relays everything
9
+ // between them and Vitest: messages, standard output and error, signals and
10
+ // exits. vitestStrongReporter.mjs starts it and stands a ChildProcess-like
11
+ // object in for each worker.
12
+ //
13
+ // It also keeps workers started ahead of time. A copy's worker spends its
14
+ // first few hundred milliseconds starting Node, loading Vitest's worker and,
15
+ // where the project tests in a DOM environment, loading that environment's
16
+ // package (jsdom: 300 ms on tRPC). None of it depends on the copy: it is what
17
+ // every worker of the project does before Vitest tells it anything. So a
18
+ // worker is started with the same entry, arguments and options Vitest gives
19
+ // its workers, loads what a fresh worker would load in the order it would,
20
+ // and waits. Vitest's next request for a worker gets it, and its copy starts
21
+ // at once. The same file, run with SUPERCOV_STRONG_WORKER set, is that
22
+ // worker's entry.
23
+ import { fork } from "node:child_process";
24
+ import { fileURLToPath, pathToFileURL } from "node:url";
25
+
26
+ const workerSpec = process.env["SUPERCOV_STRONG_WORKER"];
27
+ if (workerSpec)
28
+ await startAhead(JSON.parse(workerSpec));
29
+ else
30
+ spawn();
31
+
32
+ // A worker started ahead: from here on the process is the one Vitest would
33
+ // have forked, waiting for its first message.
34
+ async function startAhead({ entry, args, preload }) {
35
+ delete process.env["SUPERCOV_STRONG_WORKER"];
36
+ process.argv.splice(1, process.argv.length - 1, entry, ...args);
37
+ await import(pathToFileURL(entry).href);
38
+ // A fresh worker loads its environment's package once Vitest has told it
39
+ // which: after its entry, before any test file.
40
+ // Vitest passes --experimental-import-meta-resolve, without which Node
41
+ // resolves against this file instead of Vitest's: not the module Vitest
42
+ // would load.
43
+ if (process.execArgv.includes("--experimental-import-meta-resolve")) {
44
+ for (const name of preload) {
45
+ try {
46
+ await import(import.meta.resolve(name, pathToFileURL(entry).href));
47
+ }
48
+ catch {
49
+ // Vitest loads it itself, and reports what fails.
50
+ }
51
+ }
52
+ }
53
+ process.send({ supercovStrongReady: true });
54
+ }
55
+
56
+ // The DOM environments Vitest has built in, and the package each loads.
57
+ const ENVIRONMENT_PACKAGES = { jsdom: "jsdom", "happy-dom": "happy-dom" };
58
+
59
+ function spawn() {
60
+ const children = new Map();
61
+ const tell = (record) => {
62
+ if (process.connected)
63
+ process.send(record);
64
+ };
65
+ // Workers started ahead, by what Vitest forks them with: its worker entry,
66
+ // arguments and options (every worker of a project gets the same).
67
+ const pools = new Map();
68
+ // How many workers each pool keeps started (the reporter says).
69
+ let poolSize = 0;
70
+ let startedAhead = 0;
71
+ let usedAhead = 0;
72
+
73
+ // A worker's output and exit go to Vitest under the id it asked for.
74
+ const relay = (id, child) => {
75
+ children.set(id, child);
76
+ child.on("message", (message) => tell({ id, message }));
77
+ child.on("error", (error) => tell({ id, error: { message: String(error?.message ?? error), code: error?.code } }));
78
+ child.stdout?.on("data", (data) => tell({ id, stdout: data }));
79
+ child.stderr?.on("data", (data) => tell({ id, stderr: data }));
80
+ // `close` comes after the process's output: all of it was relayed.
81
+ child.on("close", (code, signal) => {
82
+ children.delete(id);
83
+ tell({ id, exit: [code, signal] });
84
+ });
85
+ };
86
+
87
+ const refill = (pool) => {
88
+ while (pool.ready.length + pool.starting.size < poolSize) {
89
+ const { modulePath, args, options } = pool.request;
90
+ let child;
91
+ try {
92
+ child = fork(fileURLToPath(import.meta.url), [], {
93
+ ...options,
94
+ env: {
95
+ ...(options.env ?? process.env),
96
+ SUPERCOV_STRONG_WORKER: JSON.stringify({ entry: modulePath, args, preload: [...pool.preload] }),
97
+ },
98
+ });
99
+ }
100
+ catch {
101
+ return;
102
+ }
103
+ startedAhead += 1;
104
+ const waiting = { child, output: [] };
105
+ pool.starting.add(waiting);
106
+ const stdout = (data) => waiting.output.push(["stdout", data]);
107
+ const stderr = (data) => waiting.output.push(["stderr", data]);
108
+ const ignore = () => { };
109
+ const ready = (message) => {
110
+ if (!message?.supercovStrongReady)
111
+ return;
112
+ child.off("message", ready);
113
+ pool.starting.delete(waiting);
114
+ pool.ready.push(waiting);
115
+ };
116
+ // Ended before Vitest took it: not one of Vitest's workers.
117
+ const ended = () => {
118
+ pool.starting.delete(waiting);
119
+ const at = pool.ready.indexOf(waiting);
120
+ if (at >= 0)
121
+ pool.ready.splice(at, 1);
122
+ };
123
+ child.stdout?.on("data", stdout);
124
+ child.stderr?.on("data", stderr);
125
+ child.on("error", ignore);
126
+ child.on("message", ready);
127
+ child.on("exit", ended);
128
+ waiting.release = () => {
129
+ child.stdout?.off("data", stdout);
130
+ child.stderr?.off("data", stderr);
131
+ child.off("error", ignore);
132
+ child.off("message", ready);
133
+ child.off("exit", ended);
134
+ };
135
+ }
136
+ };
137
+
138
+ // Vitest's worker for request `id`: one started ahead where one is ready,
139
+ // else a new one.
140
+ const take = (request) => {
141
+ const { id } = request;
142
+ const key = JSON.stringify([request.modulePath, request.args, request.options]);
143
+ let pool = pools.get(key);
144
+ if (!pool) {
145
+ pool = { request, ready: [], starting: new Set(), preload: new Set(), asked: 0 };
146
+ pools.set(key, pool);
147
+ }
148
+ pool.asked += 1;
149
+ const waiting = pool.ready.shift();
150
+ if (waiting) {
151
+ usedAhead += 1;
152
+ const { child, output } = waiting;
153
+ waiting.release();
154
+ relay(id, child);
155
+ tell({ id, spawned: child.pid });
156
+ for (const [stream, data] of output)
157
+ tell({ id, [stream]: data });
158
+ }
159
+ else {
160
+ let child;
161
+ try {
162
+ child = fork(request.modulePath, request.args, request.options);
163
+ }
164
+ catch (error) {
165
+ tell({ id, error: { message: String(error?.message ?? error), code: error?.code } });
166
+ tell({ id, exit: [null, null] });
167
+ return;
168
+ }
169
+ relay(id, child);
170
+ child.on("spawn", () => tell({ id, spawned: child.pid }));
171
+ }
172
+ // Options Vitest gives one worker only are not worth workers waiting.
173
+ if (pool.asked > 1)
174
+ refill(pool);
175
+ };
176
+
177
+ const stopAhead = () => {
178
+ for (const pool of pools.values()) {
179
+ for (const { child } of [...pool.ready, ...pool.starting])
180
+ child.kill("SIGKILL");
181
+ pool.ready = [];
182
+ pool.starting.clear();
183
+ }
184
+ };
185
+
186
+ process.on("message", (request) => {
187
+ const { id } = request;
188
+ if (request.fork) {
189
+ take(request);
190
+ return;
191
+ }
192
+ if (request.pool !== undefined) {
193
+ poolSize = request.pool;
194
+ if (!poolSize)
195
+ stopAhead();
196
+ for (const pool of pools.values())
197
+ if (pool.asked > 1)
198
+ refill(pool);
199
+ return;
200
+ }
201
+ // The environment Vitest starts its workers in (its first message to
202
+ // one says): a worker started from now on loads its package too.
203
+ if (request.environment !== undefined) {
204
+ const name = ENVIRONMENT_PACKAGES[request.environment];
205
+ for (const pool of pools.values()) {
206
+ if (name && pool.request.modulePath === request.modulePath && !pool.preload.has(name)) {
207
+ pool.preload.add(name);
208
+ // Those started without it are not what Vitest would start now.
209
+ for (const { child } of [...pool.ready, ...pool.starting])
210
+ child.kill("SIGKILL");
211
+ pool.ready = [];
212
+ pool.starting.clear();
213
+ if (pool.asked > 1)
214
+ refill(pool);
215
+ }
216
+ }
217
+ return;
218
+ }
219
+ if (request.stats) {
220
+ tell({ stats: { startedAhead, usedAhead } });
221
+ return;
222
+ }
223
+ const child = children.get(id);
224
+ if (!child)
225
+ return;
226
+ if (request.send) {
227
+ if (child.connected)
228
+ child.send(request.message, (error) => {
229
+ if (error)
230
+ tell({ id, error: { message: String(error.message), code: error.code } });
231
+ });
232
+ else
233
+ tell({ id, error: { message: "Channel closed", code: "ERR_IPC_CHANNEL_CLOSED" } });
234
+ }
235
+ else if (request.kill)
236
+ child.kill(request.kill);
237
+ else if (request.disconnect && child.connected)
238
+ child.disconnect();
239
+ });
240
+
241
+ // Vitest's process is gone: so are its workers.
242
+ process.on("disconnect", () => {
243
+ stopAhead();
244
+ for (const child of children.values())
245
+ child.kill("SIGKILL");
246
+ process.exit(0);
247
+ });
248
+ }